Data File - File Handling in C++

File Handling in C++

  • get ( ) function:

Prototypes are :

istream & get (char * buf, int num, char delim = '\n') ;

The above first form reads characters into a character array pointed to by buf until either num characters have been read, or the character specified by delim has been encountered. For instance,

char line ; cin.get (line, 40, '$') ;

The above statements will read characters into line until either 40 characters are read or '$' character is encountered, whichever occurs earlier. If the input given in rtesponse to above statements is as follows :

Value is $ 177.5

Then line will be storing

Value is

And if the input given is as follows :

The amount is 17.5.

The contents of line will be

The amount is 17.5.

The array pointed to by buf will be null-terminated by get ( ). If no delim character is specified, by default a newline character acts as a delimiter. If the delimiter character is encountered in the input stream the get ( ) function does not extract it. Rather, the delimiter character remains in the stream until the next input operation.

int get ( ) ;

The above second form of get ( ) returns the next character from the stream. It returns EOF if the end-of-file is encountered. For instance, the following code fragment illustrates it :

int i ; char ch ; ch = i = fin.get ( ) ;

If the input given is A, then the value of i will be 65(ASCII value of A) and the value of ch will be A.

  • getline ( ) function

Prototype is :

istream & getline (char * buf, int num, char delim = '\n') ;

This function is virtually identical to get(buf, num, delim) version of get ( ). The difference between get(buf, num, delim) and getline ( ) is that getline ( ) reads and removes the delimiter newline character from the input stream if it is encountered which is not done by the get ( ) function. Following figure explains the difference between get ( ) and getline ( ) functions :

  • read ( ) and write ( ) functions :

Reading and writing blocks of binary data is to use C++'s read ( ) and write ( ) functions. Their prototypes are :

istream & read ( (char *) & buf, int sizeof (buf)) ; ostream & write ( (char *) & buf, int sizeof (buf)) ;

The read ( ) function reads sizeof(buf) bytes from the associated stream and puts them in the buffer pointed to by buf. The write ( ) function writes sizeof(buf) bytes to the associated stream from the buffer pointed to by buf. The data written to a file using write ( ) can only be read accurately using read ( ). The following program writes a structure to the disk and then reads it back using write ( ) and read ( ) functions.

#include #include #include // for clrscr ( ) struct customer { char name ; float balance ; }; int main ( ) { clrscr ( ) ; customer savac; strcpy(savac.name, "Tina Marshall") ; // copy value to structure savac.balance = 21310.75 ; // variable savac ofstream fout ; fout.open("Saving", ios :: out | ios :: binary) ; // open output file if(!fout) { cout << "File can't be opened \n" ; return 1; } fout.write((char *) & savac, sizeof(customer)) ; // write to file fout.close ; // close connection // read it back now ifstream fin ; fin.open("Saving", ios :: out | ios :: binary) ; // open input file fin.read((char *) & savac, sizeof(customer)) ; // read structure cout << savac.name ; // display structure now cout << "has the balance amount of Rs." << savac.balance << "\n" ; fin.close( ); }

As you can see, only a single call to read ( ) or write ( ) is necessary to read or write the entire structure. Each individual field need not be read or written separately. If the end-of-file is reached before the specified number of characters have been read, the read ( ) simply stops, and the buffer contains as many characters as were available.

Read more about this topic:  Data File

Famous quotes containing the words file and/or handling:

    Probably nothing in the experience of the rank and file of workers causes more bitterness and envy than the realization which comes sooner or later to many of them that they are “stuck” and can go no further.
    Mary Barnett Gilson (1877–?)

    Many more children observe attitudes, values and ways different from or in conflict with those of their families, social networks, and institutions. Yet today’s young people are no more mature or capable of handling the increased conflicting and often stimulating information they receive than were young people of the past, who received the information and had more adult control of and advice about the information they did receive.
    James P. Comer (20th century)