首页 > 技术文章 > C++ 按行读取文件并打印

shenji 2020-02-19 10:26 原文

#include<iostream>
#include<fstream>
#include<string>
#include <vector>
#include <vector>

using namespace std;

void write_file()
{
    string ttt;
    cin >> ttt;
    ofstream aa;
    aa.open("c:/123.txt");
    aa << ttt << endl;
    aa.close();
}

void read_file()
{
    ifstream aa;
    aa.open("c:/123.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()
{    
    write_file(); //写入文件
    read_file();//按行读取文件中的所有数据
    return 0;
}

 

推荐阅读