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.
#includeAs 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:
“I have been a soreheaded occupant of a file drawer labeled Science Fiction ... and I would like out, particularly since so many serious critics regularly mistake the drawer for a urinal.”
—Kurt Vonnegut, Jr. (b. 1922)
“That a good fit between parental handling and child temperament is vital to help children adapt to the imperatives of their society is a crucial concept that can be applied to other cultures.”
—Stella Chess (20th century)