Content Frame
Note for screen reader users: There is text between the form elements on this page. To be sure that you do not miss any text, use item by item navigation methods, rather than tabbing from form element to form element.
Skip Breadcrumb Navigation
Home  arrow Student Resources  arrow Quizzes  arrow Chapter 12

Chapter 12

Streams and File I/O

This activity contains 10 questions.

Question 1.
The following code intends to read an integer from the file "foo.txt", but has an error. What is it?

ofstream f;
int i;

f.open("foo.txt");
f >> i;
cout << i << endl;
f.close();

 
End of Question 1


Question 2.
The following code intends to read an integer from the file "foo.txt" in the root of the C:\ drive, but it has an error. What is it?

ifstream f;
int i;

f.open("c:\foo.txt");
f >> i;
cout << i << endl;
f.close();

 
End of Question 2


Question 3.
The program below intends to add log messages to a log file. Will this work?

using namespace std;

int main()
{
	ofstream f;
	string message;

	message = "Message to add to the log file";
	f.open("log.txt");
	f << message.c_str() << endl;
	f.close();
	return 0;
}

 
End of Question 3


Question 4.

Say that we have the file named "foo.txt" in the current directory that contains:

10
20

The code below attempts to read this file as follows, except the file name is misspelled:

ifstream f;
int i=1;

f.open("foobar.txt");    
f >> i;
cout << i << endl;
f.close();

What will the program output when run?
 
End of Question 4


Question 5.

You have a file of names stored in the file "names.txt". It is organized so that the first line is a number indicating how many names are in the file, followed by the actual names. For example the file could contain the following contents:

4
Fresco, Al
Guini, Lynn
Oki, Kerry
Wright, Eaton

Which program will open the file and correctly input each name into an array?
 
End of Question 5


Question 6.
You have a file of names stored in the file "names.txt". Each name is on a separate eline. For example the file could contain the following contents:

Fresco, Al
Guini, Lynn
Oki, Kerry
Wright, Eaton

Which program will open the file and correctly input each name into a vector?

 
End of Question 6


Question 7.

What is the output of the following code?

int i;
	
cout.setf(ios::fixed | ios::showpoint | ios::left);
cout.width(10);
cout << "Number";
cout.width(6);
cout << "Amount" << endl;
cout.precision(3);	
for (i=0; i<5; i++) {
	cout.width(10);
	cout << i;
	cout.width(6);
	cout << i*0.1 << endl;
}
 
End of Question 7


Question 8.
Is it possible to directly store binary data to the middle of a file?

 
End of Question 8


Question 9.
What is the relationship between the class istream and the class ifstream?

 
End of Question 9


Question 10.

The code skeleton below opens a file for output and will loop forever. Inside the loop the program will periodically output some data to the file.

ofstream f;

f.open("log.txt", ios::app);
while (true)
{
	// Some code here to periodically 
	// set the variable message
	f << message << endl;
}

Upon opening the file "log.txt" on the disk, you find that nothing has been appended to the file as expected. What is a likely reason?
 
End of Question 10





Pearson Copyright © 1995 - 2010 Pearson Education . All rights reserved. Pearson Addison Wesley is an imprint of Pearson .
Legal Notice | Privacy Policy | Permissions

Return to the Top of this Page