Database System Concepts
Database System Concepts
7th Edition
ISBN: 9780078022159
Author: Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher: McGraw-Hill Education
Bartleby Related Questions Icon

Related questions

Question

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 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.

 

 

// this program reads the entire contents of an input file and will
// display it with the same format.
#include <iostream>
#include <fstream>
#include <cstdlib>
using namespace std;
int main()
{
char c;
ifstream in_s; // declaration of the stream of type input
char input file [15];
cout << "Enter the name of the input file: ";
cin >> input_file;
in_s.open (input_file); // connect to the input file and test
if (in_s.fail())
{
cout << "Unable to open input file " << input_file << endl;
exit (EXIT_FAILURE); // exit if cannot open file
cout<<"*** Here are the contents of the input file ***"<<endl;
while (in_s>>c) // read all characters one-by-one to end of file
{
I
cout<<c;
cout<<endl<<"*** Done writing the contents of the file ***"<<endl;
in_s.close(); // close the file
return 0;
expand button
Transcribed Image Text:// this program reads the entire contents of an input file and will // display it with the same format. #include <iostream> #include <fstream> #include <cstdlib> using namespace std; int main() { char c; ifstream in_s; // declaration of the stream of type input char input file [15]; cout << "Enter the name of the input file: "; cin >> input_file; in_s.open (input_file); // connect to the input file and test if (in_s.fail()) { cout << "Unable to open input file " << input_file << endl; exit (EXIT_FAILURE); // exit if cannot open file cout<<"*** Here are the contents of the input file ***"<<endl; while (in_s>>c) // read all characters one-by-one to end of file { I cout<<c; cout<<endl<<"*** Done writing the contents of the file ***"<<endl; in_s.close(); // close the file return 0;
Expert Solution
Check Mark
Knowledge Booster
Background pattern image
Computer Science
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
Recommended textbooks for you
Text book image
Database System Concepts
Computer Science
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:McGraw-Hill Education
Text book image
Starting Out with Python (4th Edition)
Computer Science
ISBN:9780134444321
Author:Tony Gaddis
Publisher:PEARSON
Text book image
Digital Fundamentals (11th Edition)
Computer Science
ISBN:9780132737968
Author:Thomas L. Floyd
Publisher:PEARSON
Text book image
C How to Program (8th Edition)
Computer Science
ISBN:9780133976892
Author:Paul J. Deitel, Harvey Deitel
Publisher:PEARSON
Text book image
Database Systems: Design, Implementation, & Manag...
Computer Science
ISBN:9781337627900
Author:Carlos Coronel, Steven Morris
Publisher:Cengage Learning
Text book image
Programmable Logic Controllers
Computer Science
ISBN:9780073373843
Author:Frank D. Petruzella
Publisher:McGraw-Hill Education