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
fail()
returns true if the previous stream operation had an error.
stream error
occurs when insertion or extraction fails, causing the stream to enter an errorstate.
ofstream
short for “output file stream”, is a class that supports writing to a file.
extraction operator
> >
ifstream inputFile(inputFileName);
if (!inputFile.is_open()) {
cout «_space;“Failed to open the input file.” «_space;endl;
return 1;
}
What does if (!inputFile.is_open()) check? a) If the input file is open b) If the input file failed to open c) If the input file is empty d) If there was an error while reading the input file
b) If the input file failed to open
Which library is used for basic input and output operations in C++? a) iostream b) string c) fstream d) sstream
a) iostream
The getline() function is used to read a __________ from an input stream.
line
The»_space; operator is used for __________ from an input stream.
extraction
The stringstream class can be used to manipulate __________.
strings
What does the eof() function in C++ check for? a) Check if the input stream is in a fail state b) Check if the end of the file has been reached c) Check if a specific character is found in the input stream d) Check if the input stream is empty
b) Check if the end of the file has been reached
What does the following line of code do? int day = stoi(date.substr(date.find(‘ ‘) + 1, date.find(‘,’) - date.find(‘ ‘) - 1));
A) Extracts the day from the input date string B) Extracts the year from the input date string C) Converts the day string to an integer D) Converts the year string to an integer
A) Extracts the day from the input date string
What is the purpose of the following line of code? string month = date.substr(0, date.find(‘ ‘));
A) Extract the day from the input date B) Extract the month from the input date C) Extract the year from the input date D) Extract the month and day from the input date
B) Extract the month from the input date