C++ 按行读取文件并打印

#include<iostream>
#include<fstream>
#include<string>
#include <vector>
using namespace std;

int main()
{
    fstream f("file.txt");
    vector<string> words;
    string line;
    while (getline(f, line)) //按行读取文件 直到文件结束
    {
        words.push_back(line); //把读取的数据存储到words中
    }
    cout << "共有数据" << words.size() << endl;
    for (int i = 0; i < words.size(); i++)
    {
        cout << words[i] << endl;  //打印数据
    }
    f.close();//关闭文件  
    return 0;

}

猜你喜欢

转载自www.cnblogs.com/shenji/p/12330089.html