Description
5.1 C++ Stream Classes and File Stream Operations
C++ uses a set of classes for file I/O operations, which are part of the <fstream> header file. These classes are derived from the iostream classes, allowing you to use familiar operators like << and >> for file operations.
ios: The base class for all stream classes, providing basic functionalities.istream: Handles input streams.cinis an object of this class.ostream: Handles output streams.coutis an object of this class.ifstream(Input File Stream): A class for reading from files.ofstream(Output File Stream): A class for writing to files.fstream(File Stream): A class for both reading and writing to files.
5.2 End of File Detection and File Modes
Detection of End of File (EOF) The end of file is a condition that occurs when a program attempts to read past the end of a file. The eof() function, a member of the stream classes, can be used to check for this condition. It returns true if the end of the file has been reached. A more common and reliable method is to check the stream object itself in a loop, as it evaluates to false when a read operation fails (including at EOF).
File Modes File modes specify the purpose for which a file is being opened. They are defined in the ios class and can be combined using the bitwise OR operator (|).
5.3 Opening, Closing, Reading, and Writing to Files
Opening Files You can open a file in two ways:
- Using Constructors: The most common way. An
ofstreamorifstreamobject is created and initialized with the filename.ofstream outfile("data.txt");
- Using
open()function: An empty file stream object is created first, and then theopen()member function is used to associate it with a file. This is useful for opening multiple files with the same stream object.ofstream outfile; outfile.open("data.txt");
Closing Files It’s crucial to close a file after you are done with it to ensure all data is written and resources are freed. This is done using the close() function.
outfile.close();
Reading from and Writing to Files Once a file is open, you can read from it using the >> operator with ifstream objects and write to it using the << operator with ofstream objects.
Formatted Input/Output Functions in File Similar to console I/O, you can use stream manipulators (setw, setprecision, etc.) with file streams to format data during input and output.
5.4 Types of File Access
Sequential Access In sequential access, data is processed in the order it appears in the file, from beginning to end. To access a record in the middle of a file, you must read all the preceding records. This is typical for text files and is the default mode for C++ file streams.
Random Access Random access allows a program to read from or write to any location in a file without having to read from the beginning. This is achieved by using functions like seekg() (seek get) and seekp() (seek put) to move the file’s read/write pointers to a specific byte location. This is highly efficient for large databases or files where you need to frequently access non-consecutive records.
Keywords of this Topic: fstream, ifstream, ofstream, open, close, eof, ios::in, ios::out, ios::app, ios::trunc, seekg, seekp.





Reviews
There are no reviews yet.