Recall & Review
beginner
What header file is needed to read from and write to files in C++?
You need to include the <fstream> header to work with file streams in C++.
Click to reveal answer
beginner
How do you open a file for writing in C++?
Create an std::ofstream object and pass the filename to its constructor or use the open() method. For example:<br>
std::ofstream file("example.txt");Click to reveal answer
beginner
How do you check if a file was opened successfully in C++?
Use the is_open() method on the file stream object. For example:<br>
if (file.is_open()) { /* file opened */ }Click to reveal answer
beginner
What is the difference between std::ifstream and std::ofstream?
std::ifstream is used to read from files, while std::ofstream is used to write to files.
Click to reveal answer
beginner
How do you read a line of text from a file in C++?
Use std::getline() with an ifstream object and a string variable. Example:<br>
std::string line;<br>std::getline(file, line);Click to reveal answer
Which C++ class is used to write data to a file?
✗ Incorrect
std::ofstream is specifically designed for writing data to files.
How do you check if a file opened successfully in C++?
✗ Incorrect
The is_open() method returns true if the file stream is successfully opened.
Which function reads a whole line from a file into a string?
✗ Incorrect
std::getline() reads a line from the input stream into a string.
What header must be included to use file streams in C++?
✗ Incorrect
Which class can be used for both reading and writing files?
✗ Incorrect
std::fstream supports both input and output operations on files.
Explain how to open a file for reading and check if it opened successfully in C++.
Think about the steps from including the right header to checking the file state.
You got /4 concepts.
Describe how to write text to a file and then close it properly in C++.
Remember the sequence: open, write, close.
You got /4 concepts.