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
- Quản lý quyền truy cập của người dùng trong Microsoft SQL Server
- Vacation Vocabulary: Essential English Words for Your Next Trip
- 6 công nghệ trong điện thoại bạn dùng mỗi ngày (mà ít khi biết nó hoạt động thế nào)
- Trạng thái, Xu hướng và Hệ sinh thái Giao diện Não-Máy tính (BCI) Tiêu dùng (2025-2035)
- Năm học 2026-2027: BHYT học sinh, sinh viên có gì mới?

