要读取的文件放在.sln的问文件夹下
#include<iostream>
#include<fstream>
#include<string>
#include<vector>
using namespace std;
void read_file()
{
ifstream aa;
aa.open("busroute.txt");
vector<string> tt;
string test;
while (getline(aa, test))
{
tt.push_back(test);
}
for (int i = 0; i < tt.size(); i++)
{
cout << tt[i] << endl; //打印数据
}
aa.close();
}
int main()
{
read_file();//按行读取文件中的所有数据
return 0;
}


输入结果

这段代码展示了如何使用C++标准库中的ifstream类来读取名为'busroute.txt'的文件内容,并逐行打印。程序首先打开文件,然后通过getline函数将每一行内容存储到字符串向量中,最后遍历并输出所有读取的行。
2487

被折叠的 条评论
为什么被折叠?



