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


#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;
}

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM