← Back to C++ Course | Chapter 15: File I/O | Lesson 2 of 6

C++ ifstream & ofstream

Writing with ofstream

std::ofstream is specialized for writing to files, and reuses the insertion operator (<<) you already know from std::cout -- once a file is open, file << "text" writes to it exactly the same way cout << "text" writes to the console.

Example: Writing with ofstream

cpp
#include <iostream>
#include <fstream>

int main() {
	std::ofstream file("data.txt");
	file << "Hello file";
	file.close();
	std::cout << "Written" << std::endl;
	return 0;
}

Reading with ifstream

std::ifstream is specialized for reading from files, using the extraction operator (>>) just like std::cin. It reads whitespace-delimited tokens one at a time -- numbers, words -- converting them to the target variable's type automatically.

Example: Reading with ifstream

cpp
#include <iostream>
#include <fstream>

int main() {
	std::ofstream out("data.txt");
	out << "42";
	out.close();
	std::ifstream in("data.txt");
	int value;
	in >> value;
	std::cout << value << std::endl;
	return 0;
}

Reading Line by Line

Because >> stops at whitespace, it can't read a line containing spaces in one call -- reading "Hello World" would stop at "Hello". std::getline(stream, str) reads an entire line, including internal spaces, up to the next newline, which is what you want for sentence- or record-based file formats.

Example: Reading Line by Line

cpp
#include <iostream>
#include <fstream>
#include <string>

int main() {
	std::ofstream out("data.txt");
	out << "Hello World";
	out.close();
	std::ifstream in("data.txt");
	std::string line;
	std::getline(in, line);
	std::cout << line << std::endl;
	return 0;
}

Appending with ofstream

Opening a file with plain ofstream truncates it, discarding any existing content before writing begins. Passing the ios::app flag instead positions every write at the end of the existing file, letting you add new data (like log entries) without destroying what was already saved.

Example: Appending with ofstream

cpp
#include <iostream>
#include <fstream>

int main() {
	std::ofstream file("data.txt", std::ios::app);
	file << "Appended text";
	file.close();
	std::cout << "Appended without truncating" << std::endl;
	return 0;
}

Safe File Writing Practices

Beyond just checking is_open(), inspect the stream's state after each read or write -- a failed write (for example, from a full disk) sets the fail bit even though the stream opened successfully, and code that doesn't check this can silently lose data.

Example: Safe File Writing Practices

cpp
#include <iostream>
#include <fstream>

int main() {
	std::ofstream file("data.txt");
	file << "Some data";
	if (file.fail()) {
		std::cout << "Write failed" << std::endl;
	} else {
		std::cout << "Write succeeded" << std::endl;
	}
	file.close();
	return 0;
}
🔒

Chapter Quiz — Complete all 6 topics to unlock

0/6 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.