File Handling in C++ Opening and Closing Files, Modes, File Poin ters , Sequential Acces s [Basit Rehman 090] [Ali Buniyaadi 014]
1. Opening and Closing Files in C++ Files are opened using open() and closed using close(). In C++, file handling is done using ifstream , ofstream , and fstream classes. It is important to close the file to prevent memory/resource leakage. Example: #include <iostream> #include < fstream > using namespace std; int main() { ofstream file("example.txt"); file << "Hello, World!"; file.close (); return 0; }
Opening of File : Using Constructor : File can be open directly with constructor by passing the file name and it has an optional parameter which is mode of the file . Syntax : ofstream outfile (filename , [mode : ios ::out]) // Opens file for writing ifstream infile (filename , [mode : ios ::in]) // Opens file for reading fstream file(filename , [mode : ios :: in|ios ::out]) // Opens for both reading and writing
2. Modes and Methods in File Handling Different modes available in file handling include: ios ::in – Opens the file for reading. ios ::out – Opens the file for writing. ios ::app – Opens the file in append mode. ios ::binary – Opens the file in binary mode. Example: #include <iostream> #include < fstream > using namespace std; int main() { fstream file; file.open ("example.txt", ios ::out | ios ::app); file << "Appending new content."; file.close (); return 0; }
3. File Pointers and Manipulators File pointers manage the current position within the file: seekg () – Moves the get pointer (reading). seekp () – Moves the put pointer (writing). tellg () – Returns the position of the get pointer. tellp () – Returns the position of the put pointer. Example: #include <iostream> #include < fstream > using namespace std; int main() { ifstream file("example.txt"); file.seekg (0, ios ::beg); long position = file.tellg (); cout << "Position: " << position << endl ; file.close (); return 0; }
4. Sequential Access to Files Sequential access involves reading or writing data one line at a time. Example using getline (): #include <iostream> #include < fstream > using namespace std; int main() { ifstream file("example.txt"); string line; while ( getline (file, line)) { cout << line << endl ; } file.close (); return 0; }
Key Points to Remember 1. Always close files after operations. 2. File modes like ios::in, ios::out, and ios::app control file access. 3. File pointers allow random access using functions like seekg() and tellg(). 4. Sequential access reads data in the order it's stored.
MCQs on File Handling in C++ 1. What is the correct way to open a file for writing? a) ifstream file("example.txt") b) ofstream file("example.txt") c) fstream file("example.txt", ios ::in) d) fstream file("example.txt", ios ::binary) 2. Which mode opens a file in append mode? a) ios ::in b) ios ::out c) ios ::app d) ios ::binary 3. Which function moves the file pointer to a specific location? a) seekg () b) tellg () c) close() d) getline () 4. What does the tellg () function do? a) Moves the file pointer to a specific location b) Returns the current position of the get pointer c) Writes data to the file d) Opens a file in binary mode 5. Which function is used for reading an entire line from a file? a) getline () b) >> c) put() d) tellp ()