XII-B-CS (A NOTE ON --DATA FILE HANDLING)
Data File Handling
Using stream I/O classes :So far we have performed input or output of information in our programs by using
cin and cout. These are used to handle console based I/O ( cin-- console based input stream object , cout - console based output stream object )
cin allowed us to read data from an input stream that connects the keyboard to our programs.
In a similar manner,cout allowed us to write information to an output stream that connects our program to the monitor screen.
C++ implements these file input and output streams using the subclasses ifstream and ofstream, respectively, where the subclass ifstream is derived from the istream class and the subclass ofstream is derived from the ostream class. The ifstream and ofstream classes inherit all the stream operations of the istream and ostream classes, but they also have their own member functions such as open() and close() and control their relationship to files.
Use fstream to create file stream objects that allow input and output to the same file. Conceptually it is equivalent to bi-directional "stream" connecting our program to a file.
Using File I/O: The Language like C/C++ treat everything as a file , these languages treat keyboard , mouse, printer, Hard disk , Floppy disk and all other hardware as a file.
The Basic operation on text/binary files are: Reading/writing, reading and manipulation of data stored on these files. Both type of files needs to be open and close.
NOTE: (a) Mode are optional and given at the end .
(b) Filename must follow the convention of 8.3 and it‟s extension can be anyone
How to close a file:
All types of files can be closed using close( ) member function
Syntax
Examplefin.close( ); // here fin is an object of istream class
Reading data from a Text File:
***********************THE END**********************************
Using stream I/O classes :So far we have performed input or output of information in our programs by using
cin and cout. These are used to handle console based I/O ( cin-- console based input stream object , cout - console based output stream object )
cin allowed us to read data from an input stream that connects the keyboard to our programs.
In a similar manner,cout allowed us to write information to an output stream that connects our program to the monitor screen.
Using File I/O: The Language like C/C++ treat everything as a file , these languages treat keyboard , mouse, printer, Hard disk , Floppy disk and all other hardware as a file.
The Basic operation on text/binary files are: Reading/writing, reading and manipulation of data stored on these files. Both type of files needs to be open and close.
How to open a File: Using member function Open( ) | Using Constructor | |
Syntax Filestream object; Object.open("filename",mode); Example ifstream fin; fin.open(“abc.txt”) |
Syntax Filestream object("filename",mode); Example
|
NOTE: (a) Mode are optional and given at the end .
(b) Filename must follow the convention of 8.3 and it‟s extension can be anyone
How to close a file:
All types of files can be closed using close( ) member function
Syntax
fileobject.close( ); Examplefin.close( ); // here fin is an object of istream class
Objective: To insert some data on a text file
Abc.txt Program | ABC.txt file contents |
#include<fstream> using namespace std; int main() { ofstream fout; fout.open("abc.txt"); fout<<"This is my first program in file handling"; fout<<"\n Hello again"; fout.close(); return 0; } |
This is my first program in file handling Hello again |
#include<fstream> #include<iostream> #include<conio.h> using namespace std; int main() { ifstream fin; char str[80]; fin.open("abc.txt"); fin>>str; // read only first //string from file cout<<"\n From File :"<<str; // as //spaces is treated as termination point getch(); return 0; } |
NOTE : To overcome this problem use fin.getline(str,79); |
Detecting END OF FILE Using EOF( ) member function | Using filestream object |
Syntax Filestream_object.eof( ); Example #include<iostream> #include<fstream> #include<conio.h> using namespace std; int main() { char ch; ifstream fin; fin.open("abc.txt"); while(!fin.eof()) // using eof() // function { fin.get(ch); cout<<ch; } fin.close(); getch(); return 0; } |
Example // detectting end of file #include<iostream> #include<fstream> #include<conio.h> using namespace std; int main() { char ch; ifstream fin; fin.open("abc.txt"); while(fin) // file object { fin.get(ch); cout<<ch; } fin.close(); getch(); return 0; } |
Some other very important member function: Member function name | Explanation |
seekg( ) |
Used to move reading pointer forward and backward Syntax fileobject.seekg( no_of_bytes,mode); Example: (a) fout.seekg(50,ios::cur); // move 50 bytes forward from current position (b) fout.seekg(50,ios::beg); // move 50 bytes forward from current beginning (c) fout.seekg(50,ios::end); // move 50 bytes forward from end . |
seekp( ) |
Used to move writing pointer forward and backward Syntax fileobject.seekp(no_of_bytes,mode); Example: (a) fout.seekp(50,ios::cur); // move 50 bytes forward from current position (b) fout.seekp(50,ios::beg); // move 50 bytes forward from current beginning (c) fout.seekp(50,ios::end); // move 50 bytes forward from end . |
tellp( ) |
It return the distance of writing pointer from the beginning in bytes Syntax Fileobject.tellp( ); Example: long n = fout.tellp( ); |
tellg( ) |
It return the distance of reading pointer from the beginning in bytes Syntax Fileobject.tellg( ); Example: long n = fout.tellg( ); |
Files MODES: File mode | Explanation |
ios::in | Input mode – Default mode with ifstream and files can be read only |
ios::out | Output mode- Default with ofstream and files can be write only |
ios::binary | Open file as binary |
ios::app | Preserve previous contents and write data at the end ( move forward only) |
ios::ate | Preserve previous contents and write data at the end.(can move forward and backward ) |
ios::nodelete | Do not delete existing file |
ios::noreplace | Do not replace file |
ios::nocreate | Do not create file |
Difference and Definition Text Files | Binary Files |
In these types of files all the data is firstly converted into their equivalent char and then it is stored in the files. | In these types of files all the data is stored in the binary format as it is stored by the operating system. So no conversion takes place. Hence the processing speed is much more than text files. |
get( ) member function | getline() function |
Get() function is used to read a single char from the input stream in text file Syntax fileobject.get(char); Example: fin.get(ch); //fin is file stream. |
Getline() function is used to read a string from the input stream in text file. Syntax fileobject.getline (string, no_of_char,delimiter ); Example fin.getline(str,80); // fin is file stream. NOTE: Delimiter is optional |