
Programs often need to store data permanently. If we store data only in variables, it is lost when the program ends.
File handling helps to:
So files provide permanent storage.
A stream is a flow of data (sequence of bytes/characters) between a program and an input/output device.
C++ uses stream-based I/O for both console and files.
cin: standard input streamcout: standard output streamcerr: standard error stream (useful for error messages)All of these belong to the iostream library.
C++ file handling uses <fstream> classes:
These classes provide functions like open(), close(), and stream operators << and >>.
Two ways to open:
ofstream out("data.txt");ofstream out; out.open("data.txt");Always close after use:
out.close();Closing ensures data is properly written and resources are released.
Common open modes:
ios::in (read)ios::out (write)ios::app (append)ios::trunc (truncate/overwrite)ios::binary (binary mode)Access the complete note and unlock all topic-wise content
It's free and takes just 5 seconds
Get instant access to notes, practice questions, and more benefits with our mobile app.
Download this note as PDF at no cost
If any AD appears on download click please wait for 30sec till it gets completed and then close it, you will be redirected to pdf/ppt notes page.
Programs often need to store data permanently. If we store data only in variables, it is lost when the program ends.
File handling helps to:
So files provide permanent storage.
A stream is a flow of data (sequence of bytes/characters) between a program and an input/output device.
C++ uses stream-based I/O for both console and files.
cin: standard input streamcout: standard output streamcerr: standard error stream (useful for error messages)All of these belong to the iostream library.
C++ file handling uses <fstream> classes:
These classes provide functions like open(), close(), and stream operators << and >>.
Two ways to open:
ofstream out("data.txt");ofstream out; out.open("data.txt");Always close after use:
out.close();Closing ensures data is properly written and resources are released.
Common open modes:
ios::in (read)ios::out (write)ios::app (append)ios::trunc (truncate/overwrite)ios::binary (binary mode)You can combine modes using |.
Example: ios::out | ios::app.
Pattern:
Concept example:
ofstream out("a.txt");out << "Hello";Pattern:
Concept:
ifstream in("a.txt");in >> word; (reads space-separated)For full line reading, we use getline().
EOF means end-of-file.
Common patterns:
while (in >> x) { ... } (good for formatted input)while (getline(in, line)) { ... } (good for line-by-line)Avoid wrong pattern:
while(!in.eof()) (often causes last line issues)Text files are easy to view/edit, but binary is efficient for structured records.
Binary read/write is done in ios::binary mode.
Conceptually:
In Sem 1, focus on idea and mode usage; detailed read()/write() can be studied later.
Always verify file open succeeded:
if(!in) or if(in.fail()))If file fails to open:
app vs trunc)getline() for full lineswhile(!eof()) loop>> when spaces exist in data (should use getline)Create stream → Open file → Check success → Read/Write → Close file
<fstream> provides file streams.in, out, app, trunc, binary.while(in>>x) or while(getline(...)). Avoid while(!eof()).From this topic
File handling provides permanent storage. Data stored in variables is temporary and lost after program ends. Files allow saving records, retrieving later, and sharing data between programs. Hence file handling is essential for real applications.
They are provided by <fstream>.
File handling is used for permanent storage of data. Data stored in variables is lost when program ends, but file data remains.
Basic steps:
open())Create stream → open → check success → read/write → close
Thus file handling helps store and retrieve data reliably.