打开文件;但是要跳过每一行
Opening a file; but skipping each line
我想知道如何在使用fstream
打开文件时跳过行。当我打开文件时,它返回所有的数字聚集在一起,如"201051535402530"
这是我的代码。
#include <iostream>
#include <vector>
#include <fstream>
#include <string>
using namespace std;
int main()
{
string testing;
ifstream myfile;
myfile.open ("inputfile");
while (!myfile.eof())
{
getline(myfile, testing);
cout << testing;
}
return 0;
}
输入文件被列为:
20
10
5
15
35
40
25
30
当getline()
读取一行时,它丢弃末尾的换行符。如果您希望在输出中打印一个,则需要自己将其放在那里:
cout << testing << endl;
相关文章:
- 用c++从输入文件中读取另一行
- 读取文件的最后一行并输入到链接列表时出错
- 我正在使用嵌套的while循环来解析具有多行的文本文件,但由于某种原因,它只通过第一行,我不知道为什么
- 如何使代码打印文本文件中的第一行?
- 如何格式化我的文本文件以使其不会一遍又一遍地重复同一行?
- 逐字读取文本文件中的每一行并转换为 int(无限循环或崩溃?
- 将字符串存储到文件下一行的变量中
- 为什么这个.c文件只有一行"../xx/xx.c"?
- 比较两个没有一行共同点的大文件
- 如何在c ++中以一行(水平)打印两个文件的数据?如给定的.这两种形式来自两个不同的文本文件
- 到达分隔符时跳到文本文件的下一行
- 从文本文件中读取一行并将其存储在 C++ 中的 2D 矢量中
- 从文本文件中随机选取一行
- 在获取从文件到矢量的每一行之后,数字将被更改或销毁
- 从文件中的一行读取特定数据-C++
- 从文本文件中的每一行读取数字,并计算数字重复的次数
- 为什么这一行不输出我的文本文件?
- 从文件读取并在一行中写入 cout
- 如何有效地读取C 中的最后一行文件
- 将一行文件读入对象中的两个变量