Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> C++

Khác biệt giữa file văn bản và file nhị phân trong C++

Chế độ văn bản (Text mode) Chế độ nhị phân (Binary mode)
Trong chế độ văn bản, các chuyển đổi ký tự được thực hiện, ví dụ: "\r\n" chuyển thành "\n" Trong chế độ nhị phân, các chuyển đổi đó không được thực hiện
Để ghi file:
ofstream ofs("file.txt");
Hoặc
ofstream ofs;
ofs.open("file.txt");
Để ghi file:
ofstream ofs("file.txt", ios::binary);
hoặc
ofstream ofs;
ofs.open("file.txt", ios::binary);
Để thêm nội dung cuối file:
ofstream ofs("file.txt", ios::app);
hoặc
ofstream ofs;
ofs.open("file.txt", ios::app);
Để thêm nội dung cuối file:
ofstream ofs("file.txt", ios::app | ios::binary);
hoặc
ofstream ofs;
ofs.open("file.txt", ios::app | ios::binary);
Để đọc file:
ifstream in("file.txt");
hoặc
ifstream in;
in.open("file.txt");
Để đọc file:
ifstream in("file.txt", ios::binary);
hoặc
ifstream in;
in.open("file.txt", ios::binary);