Site Loader

Before acquiring into this subdivision. it is recommended that you have a proper apprehension of Text file and Binary File. If any of both seems strange to you we give short information about these files: – Text file. The text file shops the informations in ASCII character. Each line of text is terminated with a particular character known as EOL ( End of Line ) character or delimiter character in text files. When this EOL character is read or written. certain internal interlingual renditions take topographic point. Binary file. The Binary File shops the informations in the same format as it is held in memory. No delimiters are used for a line and no interlingual renditions occur here in binary files. The undermentioned watercourse categories can be used in C++ to working with files: ofstream: Write on files ifstream: Read from files fstream: Both read and compose from/to files.

Opening File
To open a file we use unfastened ( ) bid as followers: ofstream cppfile ; cppfile. unfastened ( “filename” . manner ) ; In the above line: ofstream cppfile:
By utilizing ofstream we declared a file variable cppfile who used to entree file. cppfile. unfastened ( “filename” . manner ) : You can see that we use the file variablecppfile with unfastened ( ) to open a file. file name is a file which to be unfastened and modecan be the undermentioned: File manner

Uses
Ios: :app
Append to stop of file
Ios: :ate
travel to stop of file on gap
Ios: :binary
file unfastened in binary manner
Ios: :in
unfastened file for reading merely
Ios: :out
unfastened file for composing merely
Ios: :nocreate
unfastened fails if the file does non be
Ios: :noreplace
unfastened fails if the file already be
Ios: :trunc
cancel the contents of the file if it exist















When we use two manner at a clip we have to utilize operator OR ( | ) between manner. For illustration. if we want to open the file illustration. digital audiotape in binary manner to add informations we could make it by the followers: fstream myfile ;

myfile. unfastened ( “example. dat” . Ios: :out | Ios: :app | Ios: :binary ) ;
Shutting File
If you have opened any file so you must shut the opened file by following bid: myfile. near ( ) ;

INPUT AND OUTPUT OPERATION
put ( ) Function:
The map put ( ) writes a individual character to the associated watercourse.
acquire ( ) Function:
Similarly. the map get ( ) reads a individual character signifier the associated watercourse. illustration:
myfile. acquire ( ch ) ;
myfile. put ( ch ) ;





write ( ) map:
write ( ) map write blocks of binary informations.
read ( ) Function:
read ( ) map read blocks of binary informations.
illustration:
myfile. write ( ( coal * ) & A ; obj. sizeof ( obj ) ) ;
myfile. read ( ( coal * ) & A ; obj. sizeof ( obj ) ) ;





ERROR HANDLING FUNCTION

Function
RETURN VALUE AND MEANING
fail ( )
Tax returns true if a reading or composing operation fails.
eof ( )
Tax returns true if a file unfastened for reading has reached the terminal. bad ( )
Tax returns true if an invalid operation are attempted. for illustration if you are seeking to compose to a file that is non opened for composing operation or that file has no infinite left. good ( )
Tax returns true if no mistake has occurred.






File Arrows.
All Input Output watercourses objects have at least one internal watercourse arrow: ifstream is a merely similar istream. and it has a get arrow that points to the component to be read in the following input operation. ofstream is a merely similar ostream. and it has a put arrow that points to the location where the following component has to be written. fstream. inherits the get and the put arrows. from iostream.

The above internal watercourse arrows can be manipulated utilizing the undermentioned member maps:
seekg ( )
moves get arrow ( input ) to a specified location
seekp ( )
moves put arrow ( end product ) to a specified location
tellg ( )
gives the current place of the get arrow
tellp ( )
gives the current place of the put arrow







The other paradigm for above maps is:

seekg ( offset. ref_position ) ;
seekp ( offset. ref_position ) ;

The parametric quantity beginning represents the figure of bytes the file arrow is to be moved from the location specified by the parametric quantity ref_position. Theref_position takes one of the undermentioned three invariables defined in the ios category.

Ios: :beg start of the file
Ios: :cur current place of the arrow
Ios: :end terminal of the file

Datas Managing with Text file in C++

Example to compose in a text file

# include
# include
int chief ( )
{
ofstream myfile ;
myfile. unfastened ( “example. txt” ) ;
myfile





Post Author: admin