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:
“While waiting to get married, several forms of employment were acceptable. Teaching kindergarten was for those girls who stayed in school four years. The rest were secretaries, typists, file clerks, or receptionists in insurance firms or banks, preferably those owned or run by the family, but respectable enough if the boss was an upstanding Christian member of the community.”
—Barbara Howar (b. 1934)
“Many more children observe attitudes, values and ways different from or in conflict with those of their families, social networks, and institutions. Yet todays 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)