5.11: Using Files For Data Storage

25 July 2022
4.7 (114 reviews)
19 test answers

Unlock all answers in this set

Unlock answers (15)
question
Write the necessary preprocessor directive to enable the use of file streams.
answer
#include
Explanation: The preprocessor directive to enable the use of file streams is #include . This directive tells the compiler to include the standard library header file that contains the definition of the file stream objects.
question
Define an object named outfile that can be used to write data from program variables to a file.
answer
ofstream outfile;
Explanation: An object named outfile can be used to write data from program variables to a file. This object would typically be used in conjunction with an input file object, to allow for reading and writing to the same file. The outfile object would be used to write data to the file, while the input file object would be used to read data from the file.
question
Define two objects named outfile1 and outfile2 that can be used to write data from program variables to two different files.
answer
ofstream outfile1, outfile2;
Explanation: There are two ways to write data from program variables to two different files. The first way is to use the '>>' operator. This operator will append the data to the end of the file. The second way is to use the '>' operator. This operator will overwrite the data in the file.
question
Define an object named infile that can be used to read data into program variables from a file.
answer
ifstream infile;
Explanation: An object named infile can be used to read data into program variables from a file. This object would be created using the fstream class, and would be given a file name as an argument to its constructor. Once created, the object can be used to read data from the file using the >> operator.
question
Define two objects named infile1 and infile2 that can be used to read data into program variables from two different files.
answer
ifstream infile1, infile2;
Explanation: There are two objects that can be used to read data into program variables from two different files. The first object is called infile1 and the second object is called infile2. These objects can be used to read data from two different files into two different program variables.
question
Given an ifstream object named input1, associate it with a file named winterdata.txt by opening the file for reading.
answer
input1.open("winterdata.txt");
Explanation: ;The ifstream object named input1 is associated with the file named winterdata.txt by opening the file for reading. This allows the program to read data from the file winterdata.txt."
question
Given an ofstream object named output , associate it with a file named yearsummary.txt by opening the file for writing.
answer
output.open("yearsummary.txt");
Explanation: ;If you have an ofstream object named output, you can associate it with a file named yearsummary.txt by opening the file for writing. This will allow you to write to the file using the output object."
question
Given four files named winter2003.txt, spring2003.txt, summer2003.txt, fall2003.txt, define four ifstream objects named wntr, sprg, sumr, and fall, and use them, respectively, to open the four files for reading.
answer
ifstream wntr, sprg, sumr, fall; wntr.open("winter2003.txt"); sprg.open("spring2003.txt"); sumr.open("summer2003.txt"); fall.open("fall2003.txt");
question
Given four files named asiasales2009.txt, europesales2009.txt, africasales2009.txt, latinamericasales2009.txt, define four ofstream objects named asia, europe, africa, and latin, and use them, respectively, to open the four files for writing.
answer
ofstream asia("asiasales2009.txt"); ofstream europe("europsales2009.txt"); ofstream africa("africasales2009.txt"); ofstream latin("latinamericasales2009.txt");
question
Given that corpdata is an ifstream object that has been used for reading data and that there is no more data to be read, write the necessary code to complete your use of this object . corpdata
answer
corpdata.close();
Explanation: The code needed to complete the use of the corpdata object is to close the file. This can be done with the close() method.
question
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.)
answer
output.open("pi"); output << "3.14159"; output.close();
Explanation:The code needed to complete the use of the corpdata object is to close the file. This can be done with the close() method.
question
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.
answer
string name; int age; cin >> name >> age; outdata.open("outdata"); outdata << name << " " << age; outdata.close();
Explanation: outdata << name << " " << age
question
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.)
answer
cout << "tweet:"; getline(cin,tweet); output.open("mytweet"); output << tweet; output.close();
question
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.)
answer
input.open("rawdata"); input >> datum; input.close();
question
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.
answer
int int1, int2, int3; indata.open("lottowins"); indata >> int1 >> int2>> int3; cout << int1 << "n"; cout << int2 << "n"; cout << int3 << "n"; indata.close();
question
Given the availability of an ifstream object named indata and an ofstream object named outdata, write the other statements necessary to read one integer from a file called currentsales and write twice its value into a file called projectedsales. Assume that this is the extent of the input and output that this program will do.
answer
double sales=0.0; indata.open("currentsales"); outdata.open("projectedsales"); indata >> sales; outdata << sales * 2.0; indata.close(); outdata.close();
question
Given the availability of a file named numbers write the statements necessary to read an integer from standard input and then read in that many values from numbers and display their total.
answer
int number, sum=0; #include std::ifstream infile("numbers"); while (infile >> number) { sum += number; } cout << sum << endl;
question
Given a bool variable isReadable write some statements that assign true to isReadable if the file "topsecret" exists and can be read by the program and assigns false to isReadable otherwise.
answer
ifstream filename; filename.open("topsecret"); if (filename.fail()){ isReadable=false; }else{ isReadable=true; }
question
Given an int variable x write some statements that attempt to open a file named "table20" and read a value into x; if that turns out not to be possible your code should then read avalue from standard input into x.
answer
ifstream filename; filename.open("table20"); if (filename.fail()){ cin >> x; }else{ filename >> x; }