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