Chapter 12 C++ grade A question and answer
Write the necessary preprocessor directive to enable the use of file streams. - (correct
answer)#include <fstream>
Define an object named outfile that can be used to write data from program variables to
a file. - (correct answer)ofstream outfile;
Define an object named infile that can be used to read data into program variables from
a file. - (correct answer)ifstream infile;
Given an ifstream object named input1, associate it with a file named winterdata.txt by
opening the file for reading. - (correct answer)input1.open("winterdata.txt");
Given an ofstream object named output, associate it with a file named yearsummary.txt
by opening the file for writing. - (correct answer)output.open("yearsummary.txt");
Given the availability of an ofstream object named output, write the other statements
necessary to write the string "3.14159" into a file called pi. (Do not define a main
function.) - (correct answer)output.open("pi");
output << "3.14159";
output.close();
Read first a user's given name followed by the user's age from standard input. Then use
an ofstream object named outdata to write this information separated by a space into a
file called outdata. Assume that this is the extent of the output that this program will do. -
(correct answer)string name;
int age;
cin >> name >> age;
, outdata.open("outdata");
outdata << name << " " << age;
outdata.close();
Given the availability of an ofstream object named output, and a string variable name
tweet, write the other statements necessary to open a file named "mytweet", display the
prompt tweet: and then read an entire line into tweet and then write it out to the file
mytweet. (Do not define a main function.) - (correct answer)cout << "tweet:";
getline(cin,tweet);
output.open("mytweet");
output << tweet;
output.close();
Given the availability of an ifstream object named input, write the other statements
necessary to read an integer into a variable datum that has already been declared from
a file called rawdata. Assume that reading that one integer is the only operation you will
carry out with this file. (Note: write just the statements, do not define a main function.) -
(correct answer)input.open("rawdata");
input >> datum;
input.close();
Use an ifstream object named indata to read the first three integers from a file called
lottowins and write each number to standard output, on a line by itself. Assume that this
is the extent of the input that this program will do. - (correct answer)int x;
indata.open("lottowins");
indata >> x;
cout << x << endl;
indata >> x;
cout << x << endl;
indata >> x;
Write the necessary preprocessor directive to enable the use of file streams. - (correct
answer)#include <fstream>
Define an object named outfile that can be used to write data from program variables to
a file. - (correct answer)ofstream outfile;
Define an object named infile that can be used to read data into program variables from
a file. - (correct answer)ifstream infile;
Given an ifstream object named input1, associate it with a file named winterdata.txt by
opening the file for reading. - (correct answer)input1.open("winterdata.txt");
Given an ofstream object named output, associate it with a file named yearsummary.txt
by opening the file for writing. - (correct answer)output.open("yearsummary.txt");
Given the availability of an ofstream object named output, write the other statements
necessary to write the string "3.14159" into a file called pi. (Do not define a main
function.) - (correct answer)output.open("pi");
output << "3.14159";
output.close();
Read first a user's given name followed by the user's age from standard input. Then use
an ofstream object named outdata to write this information separated by a space into a
file called outdata. Assume that this is the extent of the output that this program will do. -
(correct answer)string name;
int age;
cin >> name >> age;
, outdata.open("outdata");
outdata << name << " " << age;
outdata.close();
Given the availability of an ofstream object named output, and a string variable name
tweet, write the other statements necessary to open a file named "mytweet", display the
prompt tweet: and then read an entire line into tweet and then write it out to the file
mytweet. (Do not define a main function.) - (correct answer)cout << "tweet:";
getline(cin,tweet);
output.open("mytweet");
output << tweet;
output.close();
Given the availability of an ifstream object named input, write the other statements
necessary to read an integer into a variable datum that has already been declared from
a file called rawdata. Assume that reading that one integer is the only operation you will
carry out with this file. (Note: write just the statements, do not define a main function.) -
(correct answer)input.open("rawdata");
input >> datum;
input.close();
Use an ifstream object named indata to read the first three integers from a file called
lottowins and write each number to standard output, on a line by itself. Assume that this
is the extent of the input that this program will do. - (correct answer)int x;
indata.open("lottowins");
indata >> x;
cout << x << endl;
indata >> x;
cout << x << endl;
indata >> x;