9. Streams (IC) Flashcards
What is the purpose of the following code snippet?
ifstream inputFile(inputFileName); if (!inputFile.is_open()) { cout «_space;“Failed to open the input file.” «_space;endl; return 1; }
A) Opens the input file for reading B) Checks if the input file exists C) Outputs an error message if the input file failed to open D) Terminates the program with an error code if the input file failed to open
A) Opens the input file for reading
What is the purpose of the size_t data type used in the loops? A) Storing the movie titles B) Storing the ratings C) Storing the showtimes D) Storing the sizes of containers
D) Storing the sizes of containers
How can you open a file for reading using ifstream? a) file.open(“filename.txt”, ios::out) b) file.open(“filename.txt”, ios::in) c) file.open(“filename.txt”, ios::app) d) file.open(“filename.txt”, ios::binary)
b) file.open(“filename.txt”, ios::in)
Which header file should be included to use ifstream and ofstream classes for file input and output? a) <iostream> b) <fstream> c) <string> d) <sstream></sstream></string></fstream></iostream>
b) <fstream></fstream>
What is the purpose of the input stream object in C++? a) Read input from the console b) Write output to the console c) Perform mathematical calculations d) Manipulate string data
a) Read input from the console
// Compute the average exam score and assign a letter grade double average = XXXXXXXXXX = assignLetterGrade(average); letterGrades.push_back(grade);
A) (midterm1 + midterm2 + final) / 3.0 B) (midterm1 + midterm2) / 2.0 C) (midterm1 + final) / 2.0 D) (midterm2 + final) / 2.0
A) (midterm1 + midterm2 + final) / 3.0
if (!found) { movieTitles.push_back(title.substr(0, 44)); movieRatings.push_back(rating); showtimes.push_back({ title, showtime }); } } inputFile.close();
How are the movie titles truncated to a maximum of 44 characters? A) Using the substr function B) Using the find function C) Using the getline function D) Using the setw manipulator
A) Using the substr function
for (size_t i = 0; i < showtimes.size(); i++) {
cout «_space;left «_space;setw(44) «_space;movieTitles[i] «_space;” | “ «_space;right «_space;setw(5) «_space;movieRatings[i] «_space;” | “;
for (size_t j = 1; XXXXXXX; j++) {
cout «_space;showtimes[i][j];
if (j != showtimes[i].size()-1) {
cout «_space;” “;
}
}
cout «_space;endl;
}
return 0;
}
A) i < showtimes.size() B) j < showtimes[i].size() C) j != showtimes[i].size()-1 D) movieShowtimes[0] == title
B) j < showtimes[i].size()
ostream
short for “output stream,” is a class that supports output, available via #include <iostream> and in namespace"std".</iostream>
insertion operator
«
istream
short for “input stream,” is a class that supports input. Available via#include <iostream></iostream>
manipulator
a function that overloads the insertion operator «_space;or extraction operator»_space; to adjust the way output appears.
istringstream
reads input from an associated string instead of the keyboard
eof()
returns true or false depending on whether or not the end of the stream has been reached.
ostringstream
insert characters into a string bufferinstead of the screen