|
You are in Section 4 of 9, Article 4.2 of 4.6
File Input Member Functions
Member functions for file input provide convenient file operations. In this article, we'll cover the following file input functions:
- get( )
- peek( )
- eof( )
- ignore( )
When extracting a string using a cin statement and the extraction operator >>, extraction will stop at the first white space character. When working with a string that contains nothing but characters, using cin is suitable. However, when working with a string that contains character space (considered white space), such as a string that would contain a person's full name, problems would arise.
The member function called get( ) allows for extracting an entire string, including strings that contain white space.
get( )
The get( ) function is a member function of the istream class. One version has two required parameters and one optional one. The two required parameters are a string variable (array of chars) and a length (max. number of chars to get + 1). The optional one, by default, is the new line character '\n' and is called the delimeter.
When get( ) is called during execution, characters (or chars) will be extracted from the input stream and assigned into the given string until one of the following occurs:
- there are no more characters to be extracted (end-of-file has been reached)
- the next available character for extraction would be the delimeter
- the # of chars extracted is one less than the value of the second parameter
Regardless of which condition stops the extraction, a null char ( '\0' ) will be stored in the string following the last character extracted. Note that even if the delimeter is reached, get( ) will not extract it.
Example:
Write a section of code that will store input provided by the user (user's full name) into a variable named fullName that contains at most 40 characters:
How do we use get( ) with files? All we need to do is attach the logical file name to get instead of cin.
So, if we have declared a file as myInputFile, then the following would be sufficient:
peek( )
The peek( ) function is another member function of the istream class. It is used to "peek" at or look ahead in an input stream and is commonly used to check for more data when processing files.
If the input stream contains additional data, peek( ) will return the value of the next character to be extracted; otherwise, it will return EOF, which represents end-of-file.
Example:
Write the beginning piece of a section code that will determine if there is more data on a certain line contained in a text file named gradeFile during processing:
eof( )
The eof( ) function is another member function of the input stream classes that returns true if the input stream is at the end-of-file; otherwise, it returns false. This function is commonly used to check for end-of-file during file processing.
Example:
Write the beginning piece of a section of code that will continue processing a file called gradeFile as long as the end-of-file has not been reached:
ignore( )
The ignore( ) function will ignore or "skip" over a specified amount of characters or all characters until the newline character ('\n') is found. Futhermore, the ignore( ) function is commonly used to "flush" the input stream after get( ) has been issued.
The following line of code uses the ignore( ) to "skip" over the next 100 characters until '\n' is reached:
Study the following program example to see how all of these file input functions can be used:
After a brief introduction to text files, some programmers wonder how to append data to an existing file. That is, how can data be added to a file that already has data without erasing all of the previous data.
On the surface, there seems as if there is a lot to this type of problem, but the code is actually quite simple.
Read on for more about appending files...
Next: Appending A File
You are in Section 4 of 9, Article 4.2 of 4.6
[Back to Top]
|