Deck 12: Streams and File IO

Full screen (f)
exit full mode
Question
You don't need using directives or using declarations to use the setw or setprecision manipulators.
Use Space or
up arrow
down arrow
to flip the card.
Question
Setting the width of output with call to the width member function,affects only the next output.
Question
A file is automatically closed when a program terminates,so there is never a need to close a file.
Question
cout has type ostream,i.e. ,is an output object.
Question
Setting the width of output with the manipulator setw is different from setting the width with the width member function.
Question
An istream object is already an ifstream object with some extra,added features.
Question
C++ uses the system file name for the program file name.
Question
When you use the open member function to tie a file name to a file stream,the file name is called the external file name,and the program refers to the file by the stream name.
Question
A stream is a flow of data into or out of your program.
Question
An output stream is a stream of data flowing from your program,to a file,a device of some sort such as the screen.
Question
When a program sends data from itself to the outside,say to a file or the screen,we say it is writing the data on the file or to the screen.
Question
An input stream is a stream of data flowing from your program,either to a file,or to the keyboard.
Question
The ____________ fstream member function closes a file stream.Explain why it is best to close a file when the program is through processing the data in the file.

A)close( )
B)overloaded operator <<( )
C)open( )
D)eof( )
E)flush( )
Question
When you write
ifstream inStream;
inStream.open("infile.dat");
the file,infile.dat must be located in the directory where the program is being run.
Question
cin is an output stream object of type ostream.
Question
The flush member function copies the file buffer to the file on the file medium (disk,tape,etc).
Question
When you call the setf function on an output stream,the effect is only until the next output.
Question
You get the manipulators endl and member functions setf and precision with the iostream header,but you have to include the iomanip header to get the manipulators setw and setprecision..
Question
The ____________ fstream member function opens a file stream and connects the stream variable to a physical file whose name is the argument to the function.

A)close( )
B)overloaded operator <<( )
C)open( )
D)eof( )
E)flush( )
Question
If the output is too wide for the width specified in a setw manipulator or a call to the width member function,then the output is lost.
Question
What output will be produced when the following code is executed? (Assume these lines are embedded in complete,correct programs,with proper #include directives. )
cout << "*";
cout.width(5);
cout << 123
<< "*" << 123 << "*" << endl;
cout << setw(5)<< 123 << "*" << 123 << "*" << endl;
Question
Assume you have opened and connected stream variables fileIn and fileOut.Assume further that you have finished with the input and output files.Write the statements necessary to close these files.
Question
Given the following code.The input file,in.dat,is a copy of the program code in this problem.How will the output file,out.dat,differ from the input file?
// file: testQuestion.cc
// test question: copy files: in.dat to out.dat
// in.dat is a copy of a file containing this code.
// use EOF to terminate copy loop
#include
#include // for exit()
using namespace std;
int main()
{
ifstream in;
ofstream out;
in.open("in.dat");
out.open("out.dat");
while(in >> ch)
out << ch;
return 0;
}
Question
Assume you have opened and connected stream variables fileIn and fileOut.Assume further that you have finished with input file,but want to read the input file a second time.What is necessary to set the stream state to reread the file from the start? Explain.
Question
You have a file that is not empty.You open a file stream for write,and attach the stream to the file.Then you close the file.What is now in that file?
Question
What output is produced by the following code,assuming these lines of code are embedded in a correct program?
cout << "*" << setw(5)<< 123 << "*"
<< 123 << "*" << endl;
cout.setf(ios::showpos);
cout << "*" << setw(5)<< 123 << "*"
<< 123 << "*" << endl;
cout.unsetf(ios::showpos):
cout.setf(ios::left);
cout << "*" << setw(5)<< 123 << "*"
<< setw(5)<< 123 << "*" << endl;
Question
You are writing a program.Give the necessary statements to open a file and to confirm that the file has been successfully opened for writing.Important: Why bother to test?
Question
You have a file that is not empty,and you want to preserve the contents and append to the end of the file.Give the commands necessary to open a file for appending.
Question
Write a loop that will read from the current position in a file up to the end of the current line,and send this output to the screen.You may assume that any variables you need are declared,including file variables,and that any needed files have been successfully opened.
Question
Write a program that prompts for an input file name,receives the input file name in a C-string.The program should check for errors opening a file.It is not necessary for the program to do anything other than open the file.
Question
What is sent to screen when the following is executed,assuming that these lines of code are embedded in a correct,complete program? Explain this behavior.
cout << "*" << setw(3)<< 123456 << "*" << endl;
Question
What will be the output from this code fragment if embedded in an otherwise correct and complete C++ program that is supplied the following input?
Input:
12 23
45
Code:
ifstream inStream("File.txt");
int i = 0,next;
while (cin >> next)
{
\quad i++;
\quad cout << next << end;
}
inStream.close();
cout << count << flush;
Question
Write a function that will copy the contents of file in.dat to the file out.dat.Check for successful file opening of both in.dat and out.dat.The loop that actually does the copy should terminate on end of file.
Question
What happens to output when data is sent to the output stream width that is wider than that set with the setw(int)manipulator,or equivalently,with cout.width(int)?
Question
Declare and open input file stream fileIn and output file stream fileOut.Attach these to files named input.dat and output.dat.Write #include directives for any required header files.Make certain names from namespaces are accessible.
Question
You have to #include as well as #include when you use a line of code such as
cout << setw(8)<< 123456 << endl;
but not have to #include when you do the exact equivalent thing in
cout.width(8);
cout << 123456 << endl;
Why is this?
Question
There are three calls to member functions of ostream object cout that set the format of stream output so that the format of the output is in fixed point,a decimal point always shows,and there are two places of decimals after the decimal point.Give these statements along with the necessary #include and using statements.
Question
Assume that your program opens a file stream,has a file connected,and writes to the file.What changes need to be made to make your program write to the screen? (There will be stuff you need to delete and stuff that you need to change. )
Question
What output is produced by the following code,assuming these lines of code are embedded in a correct program?
cout << "*" << setw(5)<< 123;
cout.setf(ios::left);
cout << "*" << setw(5)<< 123;
cout.setf(ios::right);
cout << "*" << setw(5)<< 123 << "*" << endl;
Question
What output is sent to the file out.dat by the following code,assuming these lines of code are embedded in a correct program?
ofstream fout;
fout.open("out.dat");
fout << "*" << setw(5)<< 123 << "*"
<< 123 << "*" << endl;
fout.setf(ios::showpos);
fout << "*" << setw(5)<< 123 << "*"
<< 123 << "*" << endl;
fout.unsetf(ios::showpos):
fout.setf(ios::left);
fout << "*" << setw(5)<< 123 << "*"
<< setw(5)<< 123 << "*" << endl;
Question
Write a code segment that will read a sentence from the keyboard,terminated with a period '.' and write the sentence to the screen with all white space (blanks,tabs,,replaced by the symbol '*'.Use library a function to carry out the determination of whether the character read is 'white space'.Show any #include files you may need.
Question
Write a void function called copy_to_screen that copies the contents of a file to the screen.The argument of the function is an ifstream object.Preconditions and postconditions follow:
Preconditions: The stream argument for the function has been connected to a file with a call to the member function open.
Postcondition: The contents of the file connected to the ifstream argument have been copied to the screen so that the screen is the same as the contents of the file.This function does not close the file.
Question
Assume the following code fragment is executed while embedded in a complete,correct program.
char c1,c2,c3,c4;
cout << "Enter a line of input \n";
cin.get(c1);
cin.get(c2);
cin.get(c3);
cin.get(c4);
cout << c1 << c2 << c3 << c4 << "END OF OUTPUT";
cout << endl;
If the dialog between user and program is as follows,give the rest of the dialog.(Here, means the user presses the return key. )Explain the results.
Enter a line of input
abc
Unlock Deck
Sign up to unlock the cards in this deck!
Unlock Deck
Unlock Deck
1/43
auto play flashcards
Play
simple tutorial
Full screen (f)
exit full mode
Deck 12: Streams and File IO
1
You don't need using directives or using declarations to use the setw or setprecision manipulators.
False
2
Setting the width of output with call to the width member function,affects only the next output.
True
3
A file is automatically closed when a program terminates,so there is never a need to close a file.
False
4
cout has type ostream,i.e. ,is an output object.
Unlock Deck
Unlock for access to all 43 flashcards in this deck.
Unlock Deck
k this deck
5
Setting the width of output with the manipulator setw is different from setting the width with the width member function.
Unlock Deck
Unlock for access to all 43 flashcards in this deck.
Unlock Deck
k this deck
6
An istream object is already an ifstream object with some extra,added features.
Unlock Deck
Unlock for access to all 43 flashcards in this deck.
Unlock Deck
k this deck
7
C++ uses the system file name for the program file name.
Unlock Deck
Unlock for access to all 43 flashcards in this deck.
Unlock Deck
k this deck
8
When you use the open member function to tie a file name to a file stream,the file name is called the external file name,and the program refers to the file by the stream name.
Unlock Deck
Unlock for access to all 43 flashcards in this deck.
Unlock Deck
k this deck
9
A stream is a flow of data into or out of your program.
Unlock Deck
Unlock for access to all 43 flashcards in this deck.
Unlock Deck
k this deck
10
An output stream is a stream of data flowing from your program,to a file,a device of some sort such as the screen.
Unlock Deck
Unlock for access to all 43 flashcards in this deck.
Unlock Deck
k this deck
11
When a program sends data from itself to the outside,say to a file or the screen,we say it is writing the data on the file or to the screen.
Unlock Deck
Unlock for access to all 43 flashcards in this deck.
Unlock Deck
k this deck
12
An input stream is a stream of data flowing from your program,either to a file,or to the keyboard.
Unlock Deck
Unlock for access to all 43 flashcards in this deck.
Unlock Deck
k this deck
13
The ____________ fstream member function closes a file stream.Explain why it is best to close a file when the program is through processing the data in the file.

A)close( )
B)overloaded operator <<( )
C)open( )
D)eof( )
E)flush( )
Unlock Deck
Unlock for access to all 43 flashcards in this deck.
Unlock Deck
k this deck
14
When you write
ifstream inStream;
inStream.open("infile.dat");
the file,infile.dat must be located in the directory where the program is being run.
Unlock Deck
Unlock for access to all 43 flashcards in this deck.
Unlock Deck
k this deck
15
cin is an output stream object of type ostream.
Unlock Deck
Unlock for access to all 43 flashcards in this deck.
Unlock Deck
k this deck
16
The flush member function copies the file buffer to the file on the file medium (disk,tape,etc).
Unlock Deck
Unlock for access to all 43 flashcards in this deck.
Unlock Deck
k this deck
17
When you call the setf function on an output stream,the effect is only until the next output.
Unlock Deck
Unlock for access to all 43 flashcards in this deck.
Unlock Deck
k this deck
18
You get the manipulators endl and member functions setf and precision with the iostream header,but you have to include the iomanip header to get the manipulators setw and setprecision..
Unlock Deck
Unlock for access to all 43 flashcards in this deck.
Unlock Deck
k this deck
19
The ____________ fstream member function opens a file stream and connects the stream variable to a physical file whose name is the argument to the function.

A)close( )
B)overloaded operator <<( )
C)open( )
D)eof( )
E)flush( )
Unlock Deck
Unlock for access to all 43 flashcards in this deck.
Unlock Deck
k this deck
20
If the output is too wide for the width specified in a setw manipulator or a call to the width member function,then the output is lost.
Unlock Deck
Unlock for access to all 43 flashcards in this deck.
Unlock Deck
k this deck
21
What output will be produced when the following code is executed? (Assume these lines are embedded in complete,correct programs,with proper #include directives. )
cout << "*";
cout.width(5);
cout << 123
<< "*" << 123 << "*" << endl;
cout << setw(5)<< 123 << "*" << 123 << "*" << endl;
Unlock Deck
Unlock for access to all 43 flashcards in this deck.
Unlock Deck
k this deck
22
Assume you have opened and connected stream variables fileIn and fileOut.Assume further that you have finished with the input and output files.Write the statements necessary to close these files.
Unlock Deck
Unlock for access to all 43 flashcards in this deck.
Unlock Deck
k this deck
23
Given the following code.The input file,in.dat,is a copy of the program code in this problem.How will the output file,out.dat,differ from the input file?
// file: testQuestion.cc
// test question: copy files: in.dat to out.dat
// in.dat is a copy of a file containing this code.
// use EOF to terminate copy loop
#include
#include // for exit()
using namespace std;
int main()
{
ifstream in;
ofstream out;
in.open("in.dat");
out.open("out.dat");
while(in >> ch)
out << ch;
return 0;
}
Unlock Deck
Unlock for access to all 43 flashcards in this deck.
Unlock Deck
k this deck
24
Assume you have opened and connected stream variables fileIn and fileOut.Assume further that you have finished with input file,but want to read the input file a second time.What is necessary to set the stream state to reread the file from the start? Explain.
Unlock Deck
Unlock for access to all 43 flashcards in this deck.
Unlock Deck
k this deck
25
You have a file that is not empty.You open a file stream for write,and attach the stream to the file.Then you close the file.What is now in that file?
Unlock Deck
Unlock for access to all 43 flashcards in this deck.
Unlock Deck
k this deck
26
What output is produced by the following code,assuming these lines of code are embedded in a correct program?
cout << "*" << setw(5)<< 123 << "*"
<< 123 << "*" << endl;
cout.setf(ios::showpos);
cout << "*" << setw(5)<< 123 << "*"
<< 123 << "*" << endl;
cout.unsetf(ios::showpos):
cout.setf(ios::left);
cout << "*" << setw(5)<< 123 << "*"
<< setw(5)<< 123 << "*" << endl;
Unlock Deck
Unlock for access to all 43 flashcards in this deck.
Unlock Deck
k this deck
27
You are writing a program.Give the necessary statements to open a file and to confirm that the file has been successfully opened for writing.Important: Why bother to test?
Unlock Deck
Unlock for access to all 43 flashcards in this deck.
Unlock Deck
k this deck
28
You have a file that is not empty,and you want to preserve the contents and append to the end of the file.Give the commands necessary to open a file for appending.
Unlock Deck
Unlock for access to all 43 flashcards in this deck.
Unlock Deck
k this deck
29
Write a loop that will read from the current position in a file up to the end of the current line,and send this output to the screen.You may assume that any variables you need are declared,including file variables,and that any needed files have been successfully opened.
Unlock Deck
Unlock for access to all 43 flashcards in this deck.
Unlock Deck
k this deck
30
Write a program that prompts for an input file name,receives the input file name in a C-string.The program should check for errors opening a file.It is not necessary for the program to do anything other than open the file.
Unlock Deck
Unlock for access to all 43 flashcards in this deck.
Unlock Deck
k this deck
31
What is sent to screen when the following is executed,assuming that these lines of code are embedded in a correct,complete program? Explain this behavior.
cout << "*" << setw(3)<< 123456 << "*" << endl;
Unlock Deck
Unlock for access to all 43 flashcards in this deck.
Unlock Deck
k this deck
32
What will be the output from this code fragment if embedded in an otherwise correct and complete C++ program that is supplied the following input?
Input:
12 23
45
Code:
ifstream inStream("File.txt");
int i = 0,next;
while (cin >> next)
{
\quad i++;
\quad cout << next << end;
}
inStream.close();
cout << count << flush;
Unlock Deck
Unlock for access to all 43 flashcards in this deck.
Unlock Deck
k this deck
33
Write a function that will copy the contents of file in.dat to the file out.dat.Check for successful file opening of both in.dat and out.dat.The loop that actually does the copy should terminate on end of file.
Unlock Deck
Unlock for access to all 43 flashcards in this deck.
Unlock Deck
k this deck
34
What happens to output when data is sent to the output stream width that is wider than that set with the setw(int)manipulator,or equivalently,with cout.width(int)?
Unlock Deck
Unlock for access to all 43 flashcards in this deck.
Unlock Deck
k this deck
35
Declare and open input file stream fileIn and output file stream fileOut.Attach these to files named input.dat and output.dat.Write #include directives for any required header files.Make certain names from namespaces are accessible.
Unlock Deck
Unlock for access to all 43 flashcards in this deck.
Unlock Deck
k this deck
36
You have to #include as well as #include when you use a line of code such as
cout << setw(8)<< 123456 << endl;
but not have to #include when you do the exact equivalent thing in
cout.width(8);
cout << 123456 << endl;
Why is this?
Unlock Deck
Unlock for access to all 43 flashcards in this deck.
Unlock Deck
k this deck
37
There are three calls to member functions of ostream object cout that set the format of stream output so that the format of the output is in fixed point,a decimal point always shows,and there are two places of decimals after the decimal point.Give these statements along with the necessary #include and using statements.
Unlock Deck
Unlock for access to all 43 flashcards in this deck.
Unlock Deck
k this deck
38
Assume that your program opens a file stream,has a file connected,and writes to the file.What changes need to be made to make your program write to the screen? (There will be stuff you need to delete and stuff that you need to change. )
Unlock Deck
Unlock for access to all 43 flashcards in this deck.
Unlock Deck
k this deck
39
What output is produced by the following code,assuming these lines of code are embedded in a correct program?
cout << "*" << setw(5)<< 123;
cout.setf(ios::left);
cout << "*" << setw(5)<< 123;
cout.setf(ios::right);
cout << "*" << setw(5)<< 123 << "*" << endl;
Unlock Deck
Unlock for access to all 43 flashcards in this deck.
Unlock Deck
k this deck
40
What output is sent to the file out.dat by the following code,assuming these lines of code are embedded in a correct program?
ofstream fout;
fout.open("out.dat");
fout << "*" << setw(5)<< 123 << "*"
<< 123 << "*" << endl;
fout.setf(ios::showpos);
fout << "*" << setw(5)<< 123 << "*"
<< 123 << "*" << endl;
fout.unsetf(ios::showpos):
fout.setf(ios::left);
fout << "*" << setw(5)<< 123 << "*"
<< setw(5)<< 123 << "*" << endl;
Unlock Deck
Unlock for access to all 43 flashcards in this deck.
Unlock Deck
k this deck
41
Write a code segment that will read a sentence from the keyboard,terminated with a period '.' and write the sentence to the screen with all white space (blanks,tabs,,replaced by the symbol '*'.Use library a function to carry out the determination of whether the character read is 'white space'.Show any #include files you may need.
Unlock Deck
Unlock for access to all 43 flashcards in this deck.
Unlock Deck
k this deck
42
Write a void function called copy_to_screen that copies the contents of a file to the screen.The argument of the function is an ifstream object.Preconditions and postconditions follow:
Preconditions: The stream argument for the function has been connected to a file with a call to the member function open.
Postcondition: The contents of the file connected to the ifstream argument have been copied to the screen so that the screen is the same as the contents of the file.This function does not close the file.
Unlock Deck
Unlock for access to all 43 flashcards in this deck.
Unlock Deck
k this deck
43
Assume the following code fragment is executed while embedded in a complete,correct program.
char c1,c2,c3,c4;
cout << "Enter a line of input \n";
cin.get(c1);
cin.get(c2);
cin.get(c3);
cin.get(c4);
cout << c1 << c2 << c3 << c4 << "END OF OUTPUT";
cout << endl;
If the dialog between user and program is as follows,give the rest of the dialog.(Here, means the user presses the return key. )Explain the results.
Enter a line of input
abc
Unlock Deck
Unlock for access to all 43 flashcards in this deck.
Unlock Deck
k this deck
locked card icon
Unlock Deck
Unlock for access to all 43 flashcards in this deck.