Bỏ qua để đến nội dung

Làm việc với file (fstream)

Thư viện <fstream> cung cấp các công cụ để đọc và ghi dữ liệu vào file, dùng tương tự như std::cin/std::cout nhưng làm việc với file thay vì bàn phím/màn hình.

#include <fstream>
#include <iostream>
int main() {
std::ofstream out_file("data.txt"); // ofstream = output file stream
if (out_file.is_open()) {
out_file << "Hello, File!" << std::endl;
out_file << "Dong thu hai" << std::endl;
out_file.close(); // Đóng file sau khi ghi xong
} else {
std::cout << "Khong the mo file de ghi" << std::endl;
}
return 0;
}
  • std::ofstream: dùng để ghi (output) dữ liệu ra file
  • Toán tử << hoạt động giống hệt với std::cout
  • Luôn kiểm tra .is_open() trước khi thao tác, và .close() sau khi xong
#include <fstream>
#include <iostream>
#include <string>
int main() {
std::ifstream in_file("data.txt"); // ifstream = input file stream
if (in_file.is_open()) {
std::string line;
while (std::getline(in_file, line)) { // Đọc từng dòng cho đến hết file
std::cout << line << std::endl;
}
in_file.close();
} else {
std::cout << "Khong the mo file de doc" << std::endl;
}
return 0;
}
#include <fstream>
#include <iostream>
#include <string>
int main() {
std::ifstream in_file("data.txt");
std::string word;
while (in_file >> word) { // Đọc từng "từ" (phân cách bởi khoảng trắng)
std::cout << word << std::endl;
}
return 0;
}

Mặc định, ofstream sẽ xóa toàn bộ nội dung cũ của file trước khi ghi. Muốn ghi thêm vào cuối file mà không mất dữ liệu cũ, dùng chế độ std::ios::app:

#include <fstream>
int main() {
std::ofstream out_file("data.txt", std::ios::app); // append mode
out_file << "Dong duoc them vao cuoi file" << std::endl;
out_file.close();
return 0;
}
#include <fstream>
int main() {
std::fstream file("data.txt", std::ios::in | std::ios::out);
// Có thể vừa đọc (in) vừa ghi (out) trên cùng một file
file.close();
return 0;
}

Kiểm tra file có tồn tại/mở thành công không

Phần tiêu đề “Kiểm tra file có tồn tại/mở thành công không”
#include <fstream>
#include <iostream>
int main() {
std::ifstream file("khong_ton_tai.txt");
if (!file) { // Cách kiểm tra ngắn gọn, tương đương !file.is_open()
std::cout << "File khong ton tai hoac khong the mo" << std::endl;
}
return 0;
}

Giống với smart pointer, ofstream/ifstream cũng tuân theo nguyên lý RAII - file sẽ tự động được đóng khi biến ra khỏi phạm vi, ngay cả khi bạn quên gọi .close():

#include <fstream>
void write_data() {
std::ofstream file("data.txt");
file << "Du lieu quan trong" << std::endl;
// Không gọi .close() - nhưng vẫn AN TOÀN, file tự đóng khi hàm kết thúc
}

Tuy vậy, gọi .close() tường minh vẫn là thói quen tốt, đặc biệt khi cần chắc chắn dữ liệu đã được ghi xong trước khi tiếp tục code khác.

  • std::ofstream để ghi file, std::ifstream để đọc file, std::fstream để làm cả hai
  • std::getline() đọc từng dòng; toán tử >> đọc từng từ (phân cách bởi khoảng trắng)
  • Dùng std::ios::app để ghi thêm vào cuối file thay vì ghi đè
  • Luôn kiểm tra .is_open() trước khi thao tác; file tự động đóng khi ra khỏi scope nhờ RAII