File Processing in C++
File Processing in C++
File Processing
A computer file
is stored on a secondary storage device (e.g., hard disk, USB pen); is permanent; can be used to provide input data to a program or receive output data from a program, or both; must be opened before it is used.
Types of Files
Two types of files.
Sequential files Random access files
Sequential files
The values only can be accessed in the same sequence in which they are stored. To use we must move through successive data items in the same order as they stored in the disk.
file (fstream)
ifstream - defines new input stream (normally associated with a file). ofstream - defines new output stream (normally associated with a file).
C++ Streams
#include <fstream> File fin Memory
ios is the base classs. istream and ostream inherit from ios. ifstream inherits from istream (and ios) ofstream inherits from ostream (and ios). iostream inherits from istream and ostream (and ios). fstream inherits from ifstram, iostream and ofstream.
Opening a File
Files are opened by creating objects of stream classes.
ifstream for input ofstream for output fstream for input/output
Declare a variable of the selected stream and pass the disk file name as the argument.
E.g., ifstream fin (MyFile.dat);
Connect the stream to a disk file using the member function open() with the argument as the disk file name.
E.g., fout.open (OutFile.txt);
Writing to a File
Use the ofstream object (say fout) to send output to the disk file just like you send output to the screen by using cout.
ofstream fout (ouput.dat); float avgSales; fout << Hello there!!\n; // Writing a message to the file output.dat fout << avgSales; // Writing the contents of the variable avgSales fout.close (); // Close the file
eof () Method
Useful when no of records stored in the file is unknown. Returns non-zero on end-of-file.
Example - Reading
#include <fstream> #include <iostream> #include <string> using namespace std; int main() { ifstream fin; string name; int age; fin.open (input.dat"); if (fin.fail()) { cout << "Failed to open the file\n"; exit(1); } else { while (!fin.eof()){ fin >> name; fin >> age; cout << name << "\t" << age << "\n"; } } fin.close(); return (EXIT_SUCCESS); }
input.dat
Sunera Kundala Chathura Nayana Lalith 29 34 55 34 23
Stream-state Methods
eof () non-zero on end-of-file. fail () non-zero if last I/O operation failed or invalid operation attempted or if theres been an unrecoverable error. bad () non-zero if invalid operation attempted or if theres been an unrecoverable error. good () non-zero if all stream state bits are zero.
Mode value ios::in ios::out ios::ate ios::app ios::trunc ios::nocreate
ios::noreplace ios::binary
23
seekg ()
ifstream
seekp ()
ofstream
tellg ()
ifstream
tellp()
ofstream
Examples
Positions to the 11th byte from the beginning
fin.seekg (10, ios::beg); or fin.seekg (10);
Action performed
Go to the beginning of the file Stay at the current position Go to the end of the file Move to (n+1) byte location in the file Move forward by n bytes from the current position Move backward by n bytes from current position Move write pointer to (n+1) byte location Move write pointer backward by n bytes
get () - read a byte and point to the next byte to read put () - write a byte and point to the next location for write read () - block reading write () - block writing