0% found this document useful (0 votes)
10 views4 pages

File Handling

File handling notes
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views4 pages

File Handling

File handling notes
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

File Handling in C++

The File Handling I

Basics of Files

• File: A file is a collection of data stored on a disk with a specific name and format.
• C++ provides file handling capabilities through the <fstream> header, which
includes three classes:
o ifstream: Input file stream for reading from files.
o ofstream: Output file stream for writing to files.
o fstream: File stream for both reading and writing.

Types of Disk I/O

• Sequential Access: Data is accessed in a specific order, starting from the


beginning.
• Random Access: Data can be accessed directly using file pointers without
following a sequential order.

Standard Input/Output

• Standard Input: Refers to the default input device, usually the keyboard. In C++, it
is represented by std::cin.
• Standard Output: Refers to the default output device, usually the console. In C++,
it is represented by std::cout.

Binary Mode and Text Mode

• Text Mode: The file is interpreted as a sequence of characters. End-of-line


characters may be translated (e.g., \n to a carriage return in some systems).
• Binary Mode: The file is treated as a sequence of bytes with no translation. Used for
non-text data such as images or executables.

File Operators

• Operators are used to manipulate file streams:


o << (Insertion): Writes data to the file.
o >> (Extraction): Reads data from the file.

End-Of-File (EOF)

• EOF: A special condition that indicates the end of a file has been reached.
• Function: eof() method can be used with file streams to check for EOF.

while (!file.eof()) {
// Read data
}

The File Handling II

Opening and Closing a File

• Opening a File:
o Use the open() function or constructor of the stream class.

ofstream outFile;
outFile.open("example.txt");

• Closing a File:
o Use the close() function to release file resources.

outFile.close();

Writing to a File

• Use the << operator to write data to the file.

ofstream outFile("example.txt");
outFile << "Hello, File!";
outFile.close();

Reading from a File

• Use the >> operator or file methods such as getline() to read data.
ifstream inFile("example.txt");
string data;
while (getline(inFile, data)) {
cout << data << endl;
}
inFile.close();

Error Conditions

• Check for errors during file operations using the following methods:
o fail(): Checks if a file operation failed.
o bad(): Checks for serious errors such as hardware failure.
o good(): Checks if the file stream is in a good state.

if (file.fail()) {
cout << "Error opening file!";
}

Record Input/Output

• Record Writing:
o Use binary mode to write structured data (e.g., objects or records).

struct Record {
int id;
char name[50];
};

ofstream outFile("records.dat", ios::binary);


Record rec = {1, "John Doe"};
outFile.write(reinterpret_cast<char*>(&rec), sizeof(rec));
outFile.close();

• Record Reading:
o Use binary mode to read structured data.

ifstream inFile("records.dat", ios::binary);


Record rec;
inFile.read(reinterpret_cast<char*>(&rec), sizeof(rec));
cout << "ID: " << rec.id << ", Name: " << rec.name << endl;
inFile.close();

Conclusion

File handling in C++ provides a robust mechanism for interacting with files. Understanding
the concepts of text and binary modes, standard I/O, and error handling ensures efficient
file operations for a variety of applications.

You might also like