Cách xử lý file đơn giản trong C++


#include<iostream>
#include<fstream>
using namespace std;
class fileProcess
{
public:
    void input()
    {
    //Phan 1: Write data to file
    //Tao doi tuong de mo file
    ofstream fo;
    fo.open("D:\\data.txt");
    //Kiem tra mo file co thanh cong?
    if(fo.is_open())
    {
        fo<<"Hello CMU_TPM5!"<<endl;
        fo<<"How are you? How have your family been? I hope you are well!"<<endl;
        fo.close();
    }
    else
        cout<<"File not found!"<<endl;
    }
    void output()
    {
    //Phan 2: Read data from file
    ifstream fi;
    fi.open("D:\\data.txt");
    string str="";
    if(fi.is_open())
    {
        while(!fi.eof())
        {
            getline(fi,str);
            cout<<str<<endl;
        }
        fi.close();
    }
    else
        cout<<"File error!"<<endl;
    }
};
int main()
{
    fileProcess f;
    f.input();
    f.output();
    return 0;
}

Trương Đình Huy