c++ fstream

c++ fstream

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

int main()
{
    string in_path = "./in.txt";
    string out_path = "./out.txt";
    vector<string> lines;
    ifstream infile;
    infile.open(in_path.c_str(),ios::in);
    if (infile)
    {
        string buff;
        while (getline(infile, buff))
        {
            lines.push_back(buff);
        }
    }
    infile.close();
    for(int i=0;i!=lines.size();i++)
    {
        cout<<lines[i]<<endl;
    }

    ofstream outfile;
    outfile.open(out_path.c_str(),ofstream::out);
    for(int i=0;i!=lines.size();i++)
    {
        outfile<<lines[i]<<endl;
    }
    outfile.close();
    return 0;
}
文章目录