while( in_s >> x) // Step
(4)-Read all numbers one-by-one to the end of the file
{
....
}
to do the reading. Let's see if we can use a similar technique to read a text file and display its contents. In order to do this, let's first create the following input file.
Exercise 8.4
Create a file called act8.txt and type the following text exactly
as it appears below into that file. You may cut and paste the following
10 lines into that file.
The following program uses a similar technique as in program P81.cpp to read the entire contents of the act8.txt file and to display its entire contents exactly as they appear above.
// P82.cpp - This program reads the entire contents of an input file
and will
// display it with the same format.
#include<iostream>
#include<fstream> // Step (1)
#include<cstdlib>
using namespace std;
void get_input_stream(ifstream& in_s);
int main( )
{
char c;
ifstream in_s; // Step (2)-B - declaration
of the stream of type input
get_input_stream(in_s);
cout << "Here are the entire contents of the input file \n";
while( in_s >> c) // Step (4)-Read all numbers one-by-one
to the end of the file.
{
cout << c;
}
cout << "\n I am done with writing the contents of the file \n";
in_s.close( ); // Step (5)-Close the connection (close the file)
return 0;
}
void get_input_stream(ifstream& in_s)
{
char input_file[15]; // Step (2)-A
cout << "Please input the input file name
\n"; // Step (3)-A Get the file name
cin >> input_file;
in_s.open(input_file); // Step (3)-B - Connect to
the input file and test
if(in_s.fail( ))
{
cout << "Input file opening
failed. \n";
exit(1); // if we couldn't
open the file to read from we exit
}
}
Exercise 8.5
Cut and paste or carefully copy the P82.cpp program into a file called
ex85.cpp. Compile the program to make sure it compile without any
error. Then run the program and use the act8.txt file as the input
file.
Does this program produce the same exact output as shown above?
What do you think the problem is? Insert you answers as comments inside your ex85.cpp file.
The get and put Member Functions
We will help you find the answer very soon. The problem is that cin does not read the white spaces, i.e., it skips blank spaces, tabs (\t), and new lines (\n). Thus, the entire text will appear in one piece without the separating spaces and new lines. In order to read and write the entire text with correct spacing, we will use a member function with the input stream. The get(c) function, where c is a character, allows us to read all characters from the file one character at a time. So to fix the above program we could simply use this function instead of the cin. We used:
while( in_s >> c) // Step (4)-Read all numbers one-by-one
to the end of the file
{
....
}
to read the contents of the file. Now we can replace the in_s >> c with in_s.get(c).
Exercise 8.6
Modify the ex85.cpp program by replacing in_s >> c with in_s.get(c)
in
the while loop. Compile, then run the program for the act8.txt input.
Call your new program ex86.cpp.
Does your program produce the correct output this time? Print the answer to this question inside the file ex85.cpp.
The member function put(c) will do the opposite of what the member function get(c) does. It writes to the output stream one character at a time. To practice with this function, in program ex85.cpp, you can replace the:
cout << c;
with
cout.put(c);
That does the same thing.
The eof Member Function
There is yet another way to read a file (any file) to the end.
The member function eof( ) can be used
with a stream of input type to determine the End-Of-File. This function
returns true when the end of the input file is reached. Thus, it
can be used in a while loop to control the looping process. In general,
you need to read one character before you check to see if the end of the
file is reached. Here is a way to use the eof(
). Please note that we haven't shown the complete program.
// Other lines of program
...
ifstream in_s;
char next_char;
// Other lines of program
...
in_s.get(next_char);
while( ! in_s.eof( ) )
{
cout << next_char;
// you could use cout.put(next_char) too
in_s.get(next_char);
}
Exercise 8.7
Modify the program ex86.cpp by using the eof(
) to read to the end of file act8.txt and to display its entire contents. Make sure to check that the displayed
contents are exactly the same and in the same format.Save code as ex87.cpp