Create a file called input9B.txt and type (or copy) the following text exactly as it appears below into that file. You may cut and paste the following 7 blue lines (including the blank line between the two paragraphs) into that file: C++ is a cross-platform language that can be used to create high-performance applications. C++ was developed by Bjarne Stroustrup, as an extension to the C language. C++ gives programmers a high level of control over system resources and memory. C++ is one of the world's most popular programming languages. C++ can be found in today's operating systems, Graphical User Interfaces, and embedded systems. C++ is an object-oriented programming language which gives a clear structure to programs and allows code to be reused, lowering development costs. The following program reads the entire contents of the input9B.txt file and attempts to display its entire contents exactly as it appears above: 3 Compile and run the program, using the input9B.txt file as the input file. Did this program produce the same exact output as shown above? What do you think the problem is? The problem is that the extraction operator 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 extraction operator. The member function eof() can be used with a stream of input type to determine the endof-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. 4 Now, modify the above program by using the get() member function instead of the extraction operator (i.e., >>) as well as the eof() member function to read until the end of the file. But instead out displaying the contents to the terminal, modify the program so that it writes to whatever output file is specified by the user using the put() function. To do this, you will need to modify the program so that it prompts the user for the filenames for two streams, one for the input and the other for the output. Note that you can remove the two cout statements in main with the "***" as these lines do not need to be written to the file. When testing, do not overwrite the input file input9B.txt, but instead use out9B.txt.
B. Member Functions get, put, fail, and eof
Create a file called input9B.txt and type (or copy) the following text exactly as it appears
below into that file. You may cut and paste the following 7 blue lines (including the blank
line between the two paragraphs) into that file:
C++ is a cross-platform language that can be used to create high-performance applications.
C++ was developed by Bjarne Stroustrup, as an extension to the C language. C++ gives
programmers a high level of control over system resources and memory.
C++ is one of the world's most popular
today's
object-oriented programming language which gives a clear structure to programs and
allows code to be reused, lowering development costs.
The following program reads the entire contents of the input9B.txt file and attempts to
display its entire contents exactly as it appears above:
3
Compile and run the program, using the input9B.txt file as the input file. Did this program
produce the same exact output as shown above? What do you think the problem is?
The problem is that the extraction operator 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 extraction
operator.
The member function eof() can be used with a stream of input type to determine the endof-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.
4
Now, modify the above program by using the get() member function instead of the
extraction operator (i.e., >>) as well as the eof() member function to read until the end of
the file. But instead out displaying the contents to the terminal, modify the program so that
it writes to whatever output file is specified by the user using the put() function.
To do this, you will need to modify the program so that it prompts the user for the filenames
for two streams, one for the input and the other for the output. Note that you can remove
the two cout statements in main with the "***" as these lines do not need to be written to
the file. When testing, do not overwrite the input file input9B.txt, but instead use out9B.txt.
Trending now
This is a popular solution!
Step by step
Solved in 3 steps with 2 images