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
- Khởi động cuộc thi Future Homes Europe 2026
- [THÔNG BÁO] – Lịch bảo vệ đồ án Capstone 1&2 và một số yêu cầu trước bảo vệ
- Giải bài toán thích ứng từ quy hoạch đến thiết kế công trình của miền Trung trước ngưỡng cực đoan khí hậu mới
- 5 phương pháp học tập hiệu quả giúp học nhanh, nhớ lâu
- Martinez của UPS Foundation thực hiện vai trò của mình một cách nghiêm túc

