What is the purpose of the fstream library in C++?
The fstream library provides classes and functions to perform file input and output operations, such as opening, reading, writing, and closing files.
Click to reveal answer
beginner
How do you open a file for writing in C++?
You create an ofstream object and pass the file name to its constructor or use the open() method. For example: std::ofstream file("example.txt");
Click to reveal answer
beginner
Why is it important to close a file after opening it?
Closing a file releases the system resources associated with it and ensures that all data is properly saved and flushed from buffers to the file.
Click to reveal answer
intermediate
What happens if you try to open a file that does not exist using ifstream?
The file will not open, and the ifstream object will be in a failed state. You can check this using is_open() or by testing the stream in a boolean context.
Click to reveal answer
beginner
Show a simple C++ code snippet to open a file, write "Hello" to it, and then close it.
std::ofstream file("greeting.txt");
if (file.is_open()) {
file << "Hello";
file.close();
}
Click to reveal answer
Which C++ class is used to read from a file?
Aiostream
Bofstream
Cifstream
Dfstream
✗ Incorrect
The ifstream class is used specifically for reading from files.
What method do you use to check if a file was successfully opened?
Ais_open()
Bopen()
Cclose()
Dread()
✗ Incorrect
The is_open() method returns true if the file is open.
What happens if you forget to close a file after writing?
AData may not be saved properly
BFile is deleted automatically
CProgram crashes immediately
DNothing happens
✗ Incorrect
Not closing a file can cause data to remain in buffers and not be saved to disk.
Which header file must you include to use file streams in C++?
A<cstdio>
B<iostream>
C<fstream.h>
D<fstream>
✗ Incorrect
The standard header for file streams is <fstream>.
How do you open a file for both reading and writing?
AUse <code>ifstream</code> only
BUse <code>fstream</code> with appropriate mode flags
CUse <code>ofstream</code> only
DUse <code>iostream</code>
✗ Incorrect
The fstream class can open files for both reading and writing by specifying mode flags.
Explain the steps to open a file, write data, and close it in C++.
Think about the order of operations and the classes used.
You got /5 concepts.
What are the consequences of not closing a file after finishing file operations?
Consider what happens to data and system resources.
You got /4 concepts.
Practice
(1/5)
1. What is the primary purpose of the open() function in C++ file handling?
easy
A. To start using a file for reading or writing
B. To close a file after use
C. To delete a file from the system
D. To rename a file
Solution
Step 1: Understand the role of open()
The open() function is used to start using a file by opening it for reading or writing.
Step 2: Differentiate from other file operations
Closing a file is done by close(), deleting or renaming files are not done by open().
Final Answer:
To start using a file for reading or writing -> Option A
Quick Check:
open() = start file use [OK]
Hint: Remember: open() starts file access, close() ends it [OK]
Common Mistakes:
Confusing open() with close()
Thinking open() deletes files
Assuming open() renames files
2. Which of the following is the correct syntax to **explicitly** open a file named "data.txt" for writing using an ofstream object named file?
easy
A. file.open("data.txt", ios::in);
B. file.open("data.txt");
C. file.close("data.txt");
D. file.open("data.txt", ios::out);
Solution
Step 1: Identify the correct mode for writing
To open a file for writing, use ios::out mode with open().
Step 2: Check syntax correctness
file.open("data.txt", ios::out); correctly opens the file for writing. file.open("data.txt", ios::in); uses ios::in which is for reading. file.open("data.txt"); opens without mode, which defaults to reading for ifstream, but for ofstream it defaults to writing, but explicit mode is better. file.close("data.txt"); is incorrect syntax for closing.
Final Answer:
file.open("data.txt", ios::out); -> Option D
Quick Check:
Writing mode = ios::out [OK]
Hint: Use ios::out to open files for writing [OK]
Common Mistakes:
Using ios::in instead of ios::out for writing
Calling close() instead of open()
Omitting mode when explicit mode is needed
3. Assuming "example.txt" exists, what will be the output of the following C++ code snippet?
std::ifstream file;
file.open("example.txt");
if (!file) {
std::cout << "File not opened";
} else {
std::cout << "File opened successfully";
}
file.close();
medium
A. File not opened
B. File opened successfully
C. Compilation error
D. No output
Solution
Step 1: Understand file opening check
The code opens "example.txt" for reading. The if (!file) checks if the file failed to open.
Step 2: Assume file exists and opens
If "example.txt" exists and is accessible, file is valid, so the else block runs printing "File opened successfully".
Final Answer:
File opened successfully -> Option B
Quick Check:
File open check prints success if file opens [OK]
Hint: Check file with if(!file) after open() to confirm success [OK]
Common Mistakes:
Assuming file always fails to open
Confusing syntax of if(!file)
Forgetting to close the file
4. Identify the error in the following code snippet: