Deck 12: Streams and File IO

ملء الشاشة (f)
exit full mode
سؤال
You don't need using directives or using declarations to use the setw or setprecision manipulators.
استخدم زر المسافة أو
up arrow
down arrow
لقلب البطاقة.
سؤال
Setting the width of output with call to the width member function,affects only the next output.
سؤال
A file is automatically closed when a program terminates,so there is never a need to close a file.
سؤال
cout has type ostream,i.e. ,is an output object.
سؤال
Setting the width of output with the manipulator setw is different from setting the width with the width member function.
سؤال
An istream object is already an ifstream object with some extra,added features.
سؤال
C++ uses the system file name for the program file name.
سؤال
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.
سؤال
A stream is a flow of data into or out of your program.
سؤال
An output stream is a stream of data flowing from your program,to a file,a device of some sort such as the screen.
سؤال
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.
سؤال
An input stream is a stream of data flowing from your program,either to a file,or to the keyboard.
سؤال
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( )
سؤال
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.
سؤال
cin is an output stream object of type ostream.
سؤال
The flush member function copies the file buffer to the file on the file medium (disk,tape,etc).
سؤال
When you call the setf function on an output stream,the effect is only until the next output.
سؤال
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..
سؤال
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( )
سؤال
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.
سؤال
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;
سؤال
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.
سؤال
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;
}
سؤال
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.
سؤال
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?
سؤال
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;
سؤال
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?
سؤال
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.
سؤال
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.
سؤال
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.
سؤال
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;
سؤال
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;
سؤال
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.
سؤال
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)?
سؤال
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.
سؤال
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?
سؤال
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.
سؤال
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. )
سؤال
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;
سؤال
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;
سؤال
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.
سؤال
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.
سؤال
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 Deck
1/43
auto play flashcards
العب
simple tutorial
ملء الشاشة (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.
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 43 في هذه المجموعة.
فتح الحزمة
k this deck
5
Setting the width of output with the manipulator setw is different from setting the width with the width member function.
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 43 في هذه المجموعة.
فتح الحزمة
k this deck
6
An istream object is already an ifstream object with some extra,added features.
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 43 في هذه المجموعة.
فتح الحزمة
k this deck
7
C++ uses the system file name for the program file name.
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 43 في هذه المجموعة.
فتح الحزمة
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.
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 43 في هذه المجموعة.
فتح الحزمة
k this deck
9
A stream is a flow of data into or out of your program.
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 43 في هذه المجموعة.
فتح الحزمة
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.
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 43 في هذه المجموعة.
فتح الحزمة
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.
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 43 في هذه المجموعة.
فتح الحزمة
k this deck
12
An input stream is a stream of data flowing from your program,either to a file,or to the keyboard.
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 43 في هذه المجموعة.
فتح الحزمة
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( )
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 43 في هذه المجموعة.
فتح الحزمة
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.
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 43 في هذه المجموعة.
فتح الحزمة
k this deck
15
cin is an output stream object of type ostream.
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 43 في هذه المجموعة.
فتح الحزمة
k this deck
16
The flush member function copies the file buffer to the file on the file medium (disk,tape,etc).
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 43 في هذه المجموعة.
فتح الحزمة
k this deck
17
When you call the setf function on an output stream,the effect is only until the next output.
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 43 في هذه المجموعة.
فتح الحزمة
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..
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 43 في هذه المجموعة.
فتح الحزمة
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( )
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 43 في هذه المجموعة.
فتح الحزمة
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.
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 43 في هذه المجموعة.
فتح الحزمة
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;
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 43 في هذه المجموعة.
فتح الحزمة
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.
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 43 في هذه المجموعة.
فتح الحزمة
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;
}
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 43 في هذه المجموعة.
فتح الحزمة
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.
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 43 في هذه المجموعة.
فتح الحزمة
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?
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 43 في هذه المجموعة.
فتح الحزمة
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;
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 43 في هذه المجموعة.
فتح الحزمة
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?
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 43 في هذه المجموعة.
فتح الحزمة
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.
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 43 في هذه المجموعة.
فتح الحزمة
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.
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 43 في هذه المجموعة.
فتح الحزمة
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.
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 43 في هذه المجموعة.
فتح الحزمة
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;
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 43 في هذه المجموعة.
فتح الحزمة
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;
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 43 في هذه المجموعة.
فتح الحزمة
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.
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 43 في هذه المجموعة.
فتح الحزمة
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)?
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 43 في هذه المجموعة.
فتح الحزمة
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.
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 43 في هذه المجموعة.
فتح الحزمة
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?
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 43 في هذه المجموعة.
فتح الحزمة
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.
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 43 في هذه المجموعة.
فتح الحزمة
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. )
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 43 في هذه المجموعة.
فتح الحزمة
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;
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 43 في هذه المجموعة.
فتح الحزمة
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;
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 43 في هذه المجموعة.
فتح الحزمة
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.
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 43 في هذه المجموعة.
فتح الحزمة
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.
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 43 في هذه المجموعة.
فتح الحزمة
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
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 43 في هذه المجموعة.
فتح الحزمة
k this deck
locked card icon
فتح الحزمة
افتح القفل للوصول البطاقات البالغ عددها 43 في هذه المجموعة.