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: 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: 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: std::string line; std::getline(file, line);
Click to reveal answer
Which C++ class is used to write data to a file?
Astd::ofstream
Bstd::ifstream
Cstd::fstream
Dstd::iostream
✗ Incorrect
std::ofstream is specifically designed for writing data to files.
How do you check if a file opened successfully in C++?
AUsing the open() method
BUsing the is_open() method
CUsing the close() method
DUsing the read() method
✗ 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?
Astd::getline()
Bfile.read()
Cfile.get()
Dstd::readline()
✗ Incorrect
std::getline() reads a line from the input stream into a string.
What header must be included to use file streams in C++?
A<fstream.h>
B<iostream>
C<fstream>
D<file>
✗ Incorrect
is the standard header for file stream classes.
Which class can be used for both reading and writing files?
Astd::iostream
Bstd::ifstream
Cstd::ofstream
Dstd::fstream
✗ 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.
Practice
(1/5)
1. What is the primary purpose of using ifstream in C++?
easy
A. To write data to a file
B. To read data from a file
C. To create a new file
D. To delete a file
Solution
Step 1: Understand the role of ifstream
ifstream stands for input file stream, which is used to read data from files.
Step 2: Differentiate from ofstream
ofstream is used to write data to files, so it is not the correct answer here.
Final Answer:
To read data from a file -> Option B
Quick Check:
ifstream = read file [OK]
Hint: Remember 'i' in ifstream means input (read) [OK]
Common Mistakes:
Confusing ifstream with ofstream
Thinking ifstream creates or deletes files
Mixing input and output streams
2. Which of the following is the correct way to open a file named data.txt for writing using ofstream?
easy
A. ofstream file.open("data.txt");
B. ifstream file("data.txt");
C. file.open(ofstream, "data.txt");
D. ofstream file("data.txt");
Solution
Step 1: Recognize correct ofstream syntax
To open a file for writing, you create an ofstream object and pass the filename to its constructor.
Step 2: Check other options for syntax errors
ifstream file("data.txt"); uses ifstream which is for reading, not writing. ofstream file.open("data.txt"); incorrectly calls open as a constructor. file.open(ofstream, "data.txt"); has wrong parameter order and syntax.
The code tries to write to the file before opening it, which is incorrect because the file must be opened first.
Step 2: Confirm other options
Missing semicolon after file.open() is wrong because semicolons are present. ofstream cannot write strings is false; ofstream can write strings. File should be closed before opening is incorrect; you cannot close before opening.
Final Answer:
File is opened after writing to it -> Option C
Quick Check:
Open file before writing [OK]
Hint: Always open file before writing or reading [OK]
Common Mistakes:
Writing before opening the file
Confusing order of open and close
Assuming ofstream can't write strings
5. You want to read integers from a file input.txt and write only the even numbers to even.txt. Which code snippet correctly implements this?
hard
A. std::ifstream inFile("input.txt");
std::ofstream outFile("even.txt");
int num;
while (inFile >> num) {
if (num % 2 == 0) {
outFile << num << '\n';
}
}
inFile.close();
outFile.close();
B. std::ofstream inFile("input.txt");
std::ifstream outFile("even.txt");
int num;
while (inFile >> num) {
if (num % 2 == 0) {
outFile << num << '\n';
}
}
inFile.close();
outFile.close();
C. std::ifstream inFile;
std::ofstream outFile;
inFile.open("input.txt");
outFile.open("even.txt");
int num;
while (inFile >> num) {
if (num % 2 == 1) {
outFile << num << '\n';
}
}
inFile.close();
outFile.close();
D. std::ifstream inFile("input.txt");
std::ofstream outFile("even.txt");
int num;
while (!inFile.eof()) {
inFile >> num;
if (num % 2 == 0) {
outFile << num << '\n';
}
}
inFile.close();
outFile.close();
Solution
Step 1: Check correct stream types and file opening
std::ifstream inFile("input.txt");
std::ofstream outFile("even.txt");
int num;
while (inFile >> num) {
if (num % 2 == 0) {
outFile << num << '\n';
}
}
inFile.close();
outFile.close(); correctly uses ifstream to read and ofstream to write, opening files in constructors.
Step 2: Verify reading loop and condition
std::ifstream inFile("input.txt");
std::ofstream outFile("even.txt");
int num;
while (inFile >> num) {
if (num % 2 == 0) {
outFile << num << '\n';
}
}
inFile.close();
outFile.close(); uses while (inFile >> num) which safely reads integers until EOF. It writes only even numbers (num % 2 == 0) to output.
Step 3: Identify errors in other options
std::ofstream inFile("input.txt");
std::ifstream outFile("even.txt");
int num;
while (inFile >> num) {
if (num % 2 == 0) {
outFile << num << '\n';
}
}
inFile.close();
outFile.close(); swaps stream types incorrectly. std::ifstream inFile;
std::ofstream outFile;
inFile.open("input.txt");
outFile.open("even.txt");
int num;
while (inFile >> num) {
if (num % 2 == 1) {
outFile << num << '\n';
}
}
inFile.close();
outFile.close(); writes odd numbers, not even. std::ifstream inFile("input.txt");
std::ofstream outFile("even.txt");
int num;
while (!inFile.eof()) {
inFile >> num;
if (num % 2 == 0) {
outFile << num << '\n';
}
}
inFile.close();
outFile.close(); uses while (!inFile.eof()) which can cause reading errors.
Final Answer:
Option A code snippet -> Option A
Quick Check:
Correct streams + safe read loop + even check = std::ifstream inFile("input.txt");
std::ofstream outFile("even.txt");
int num;
while (inFile >> num) {
if (num % 2 == 0) {
outFile << num << '\n';
}
}
inFile.close();
outFile.close(); [OK]
Hint: Use while(inFile >> num) and check even with num % 2 == 0 [OK]
Common Mistakes:
Swapping ifstream and ofstream
Using eof() in loop condition
Writing odd numbers instead of even
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.