Text File Input and Output in C++
C++ text file I/O uses streams from the <fstream> header: std::ifstream reads, std::ofstream writes, and std::fstream supports both.
Summary
C++ text file I/O uses streams from the <fstream> header: std::ifstream reads, std::ofstream writes, and std::fstream supports both. Construction or open() connects a stream to a file; checking if (!file) immediately afterward detects an opening failure.
A stream tracks both its file connection and operation state. Drive reading loops with the input operation itself, such as while (std::getline(file, line)). std::getline reads through a delimiter and discards it. Formatted extraction with >> parses the destination type; string extraction stops at whitespace. The insertion operator << writes formatted text.
| Opening mode | Effect |
|---|---|
| Default std::ofstream | Replaces existing contents |
| std::ios::app | Appends every write |
| std::ios::ate | Positions at the end only when opened |
Stream-state checks help detect missing files, permission problems, invalid input, and output failures. fail() detects formatting or operation failure, while bad() indicates a serious stream error. Stream destruction closes the associated file automatically through RAII, including during exception handling; close() releases it earlier.
Common Misconceptions: eof() reports a previous encounter with end-of-file rather than predicting the next read. std::ios::ate does not guarantee that every write appends. After formatted extraction, a leftover newline can cause the next std::getline to return an empty line.
🧠 Key Concepts
- File stream types
- Opening failure checks
- Operation driven loops
- Formatted extraction
- Line input
- File opening modes
- Stream error states
- RAII resource management
- Mixed input pitfalls
🧠 Quick Check
See what you remember from the summary.
Which stream class supports both text file input and output?
🧠 Flashcards Preview
Tap a card to reveal the definition.
Ready to quiz yourself?
Test what you remember with a full practice quiz on this note. Create a free account and start in seconds.
Full Notes
Read the original note content before deciding whether to save or study from it.
Text File Input and Output in C++
📘 Overview C++ text file input and output uses file streams from the <fstream> header to transfer character data between programs and files. Correct file handling requires appropriate opening modes, operation-based loop conditions, and checks for stream errors.
🧠 Key Idea A file stream maintains both a file connection and a state indicating whether operations succeed. Drive reading loops with the read operation itself, rather than checking for end-of-file before attempting a read.
⚔️ Core Details: - The <fstream> header provides std::ifstream for input, std::ofstream for output, and std::fstream for both input and output. - Construction or open() connects a stream to a filename; if (!file) immediately afterward detects an opening failure. - while (std::getline(file, line)) processes each successfully read line without storing its terminating newline. - The insertion operator writes formatted text: out << name << '\n'; sends the value of name and a newline to the output stream. - Default std::ofstream opening replaces existing contents; std::ios::app appends each write, while std::ios::ate only positions the stream at the end initially. - File stream destruction automatically closes the associated file through RAII; close() releases it earlier.
🎯 Why It Matters: - Text files provide persistent storage for configuration settings, logs, and program results beyond a single execution. - Choosing append rather than replacement mode prevents accidental loss of existing records. - Checking stream state exposes missing files, permission problems, invalid input, and output failures instead of allowing silent data loss. - RAII releases file resources automatically when execution leaves a scope, including during exception handling.
🧠 Quick Recall: - Formatted extraction - file >> value parses a value of the destination type; string extraction stops at whitespace. - std::getline - Reads through a delimiter, normally a newline, and discards that delimiter. - eof() - Reports whether a previous input operation encountered end-of-file; it does not predict whether the next read will succeed. - fail() and bad() - fail() detects formatting or operation failure; bad() indicates a serious stream error. - Mixed input pitfall - After formatted extraction, a remaining newline can make the next std::getline return an empty line.
More ways to study when you copy this note
Copy this note into your library to unlock focused practice sessions and long-term review.
Answer all questions first, then see feedback at the end — the way real exams work.
Focuses each session on what you got wrong, not what you already know.
Full timed exam with all questions, no pausing, and results at the end. Built for board exam prep.
Part of 🗄️ BS Information Systems — 1st Year
Browse this Review Set →More Computer Engineering notes
See all →More in Programming Fundamentals
See all →More from NoteLib
Browse NoteLib's public notes →Copy this note to your library and get the full Study Pack instantly — summary, key concepts, and practice quiz included.