Exam 12: Streams and File IO

arrow
  • Select Tags
search iconSearch Question
flashcardsStudy Flashcards
  • Select Tags

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,<cr> means the user presses the return key. )Explain the results. Enter a line of input abc<cr>

Free
(Essay)
4.9/5
(22)
Correct Answer:
Verified

The dialog is:
Enter a line of input
abc
END OF INPUT
The get member gets every character input,including the <CR>.

You don't need using directives or using declarations to use the setw or setprecision manipulators.

Free
(True/False)
4.8/5
(47)
Correct Answer:
Verified

False

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.

Free
(Essay)
4.8/5
(33)
Correct Answer:
Verified

void copy_to_screen (ifstream& in)
{
\quad char ch;
\quad in.get(ch):
\quad while( ! in.eof())
\quad {
\quad\quad cout << ch;
\quad\quad in.get(next);
\quad }
}
Or just slightly simpler:
void copy_to_screen (ifstream& in)
{
\quad char ch;
\quad while( in.get(ch))
\quad cout << ch;
}

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.

(True/False)
4.9/5
(43)

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;

(Essay)
4.9/5
(31)

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,<cr>,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.

(Essay)
4.8/5
(31)

C++ uses the system file name for the program file name.

(True/False)
4.9/5
(39)

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.

(Multiple Choice)
4.8/5
(35)

A file is automatically closed when a program terminates,so there is never a need to close a file.

(True/False)
4.9/5
(41)

A stream is a flow of data into or out of your program.

(True/False)
4.8/5
(41)

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;

(Essay)
4.9/5
(43)

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.

(Essay)
4.8/5
(40)

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?

(Essay)
4.9/5
(37)

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;

(Essay)
4.8/5
(29)

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.

(Essay)
4.8/5
(32)

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)?

(Essay)
4.9/5
(32)

Setting the width of output with call to the width member function,affects only the next output.

(True/False)
4.9/5
(34)

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.

(Multiple Choice)
4.9/5
(32)

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.

(Essay)
4.9/5
(41)

An input stream is a stream of data flowing from your program,either to a file,or to the keyboard.

(True/False)
4.9/5
(34)
Showing 1 - 20 of 43
close modal

Filters

  • Essay(0)
  • Multiple Choice(0)
  • Short Answer(0)
  • True False(0)
  • Matching(0)