• Wrong document? Swap it for free
  • Written by students who passed
  • Immediately available after payment
  • Read online or as PDF
Sell
Where do you study
Your language
Document preview thumbnail
Preview 2 out of 7 pages
Exam (elaborations)

Chapter 12 C++

Document preview thumbnail
Preview 2 out of 7 pages

Exam of 7 pages for the course Chapter 12 C++ at Chapter 12 C++ (Chapter 12 C++)

Content preview

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;

Document information

Uploaded on
October 26, 2024
Number of pages
7
Written in
2024/2025
Type
Exam (elaborations)
Contains
Questions & answers
$10.49

Wrong document? Swap it for free Within 14 days of purchase and before downloading, you can choose a different document. You can simply spend the amount again.
Written by students who passed
Immediately available after payment
Read online or as PDF

Seller avatar
douglasmugwe82
5.0
(1)
Sold
2
Followers
0
Items
4215
Last sold
1 year ago



Why students choose Stuvia

Created by fellow students, verified by reviews

Quality you can trust: written by students who passed their tests and reviewed by others who've used these notes.

Didn't get what you expected? Choose another document

No worries! You can instantly pick a different document that better fits what you're looking for.

Pay as you like, start learning right away

No subscription, no commitments. Pay the way you're used to via credit card and download your PDF document instantly.

Student with book image

“Bought, downloaded, and aced it. It really can be that simple.”

Alisha Student

Working on your references?

Create accurate citations in APA, MLA and Harvard with our free citation generator.

Working on your references?

Frequently asked questions