

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
cheat sheet to get success in lab exam
Typology: Cheat Sheet
1 / 2
This page cannot be seen from the preview
Don't miss anything!


In C++, file handling allows programs to store data in files and perform various operations on them. Files can be used to save program output or retrieve data for processing. A stream is a concept that represents a source or destination for data, like a file or a console. In C++, there are three main classes for file handling:
The open() function is used to open a file in different modes (read, write, append, etc.).
fstream file; file.open("example.txt", ios::out); // Opens file for writing
The << operator is used to write data into a file.
fstream file; file.open("example.txt", ios::out); file << "Hello, File Handling!"; // Writing to file file.close(); // Always close the file after use
The >> operator is used to read data from a file.
fstream file; file.open("example.txt", ios::in); char ch; while (file >> ch) { cout << ch; // Displaying file content } file.close();
4. Closing a File (close() function) It is important to close the file after performing operations using the close() function.
file.close();