Recall & Review
beginner
What is
ifstream used for in C++?<p><code>ifstream</code> is a class in C++ used to read data from files. It stands for input file stream.</p>Click to reveal answer
beginner
How do you open a file named
data.txt for reading using ifstream?You create an ifstream object and pass the filename to its constructor or use the open() method:
std::ifstream file("data.txt");
// or
std::ifstream file;
file.open("data.txt");Click to reveal answer
beginner
How can you check if a file was opened successfully with
ifstream?Use the is_open() method or check the stream in a boolean context:
if (file.is_open()) {
// file opened
}
// or
if (file) {
// file opened
}Click to reveal answer
beginner
How do you read a line of text from a file using
ifstream?Use std::getline() with the ifstream object and a string variable:
std::string line; std::getline(file, line);
Click to reveal answer
beginner
Why should you close an
ifstream after reading a file?Closing the file releases system resources and ensures all data is properly handled. Use file.close() when done reading.
Click to reveal answer
Which header file must you include to use
ifstream in C++?✗ Incorrect
The <fstream> header provides file stream classes including ifstream.
What does
ifstream file("input.txt"); do?✗ Incorrect
This line opens the file input.txt for reading using ifstream.
How do you check if the file was opened successfully?
✗ Incorrect
The is_open() method returns true if the file is open.
Which function reads a whole line from a file into a string?
✗ Incorrect
std::getline() reads a line from the file stream into a string.
What happens if you forget to close an
ifstream?✗ Incorrect
Not closing a file can waste system resources until the program ends.
Explain step-by-step how to read text from a file using
ifstream in C++.Think about opening, reading, checking, and closing the file.
You got /5 concepts.
What are the benefits of using
ifstream for file input in C++?Consider why streams are helpful for file handling.
You got /5 concepts.