This program creates a file called output.txt and writes two lines of text into it. It also checks if the file opened correctly and tells the user when done.
C++
#include <fstream>
#include <iostream>
int main() {
std::ofstream outputFile("output.txt");
if (!outputFile) {
std::cout << "Error opening file." << std::endl;
return1;
}
outputFile << "Line 1: Hello, file output!\n";
outputFile << "Line 2: Writing more text.";
outputFile.close();
std::cout << "Data written to output.txt" << std::endl;
return0;
}
OutputSuccess
Important Notes
If the file already exists, ofstream will overwrite it by default.
Use std::ios::app mode if you want to add text without erasing existing content.
Summary
ofstream lets you save text or data to files from your program.
Always check if the file opened successfully before writing.
Close the file to make sure all data is saved properly.
Practice
(1/5)
1. What is the primary purpose of the ofstream class in C++?
easy
A. To manage memory allocation
B. To read data from files
C. To create graphical user interfaces
D. To write data to files
Solution
Step 1: Understand the role of ofstream
ofstream stands for output file stream and is used to write data to files.
Step 2: Differentiate from other stream classes
Unlike ifstream which reads files, ofstream is specifically for output (writing).
Final Answer:
To write data to files -> Option D
Quick Check:
ofstream = write files [OK]
Hint: Remember 'o' in ofstream means output (write) [OK]
Common Mistakes:
Confusing ofstream with ifstream
Thinking ofstream reads files
Assuming ofstream manages memory
2. Which of the following is the correct way to open a file named data.txt for writing using ofstream?
easy
A. std::ofstream file("data.txt");
B. std::ofstream file.open("data.txt");
C. std::ofstream file = "data.txt";
D. std::ofstream file.open = "data.txt";
Solution
Step 1: Recall correct syntax for opening file with ofstream
The correct way is to create an ofstream object and pass the filename as a constructor argument.
Step 2: Analyze each option
std::ofstream file("data.txt"); uses std::ofstream file("data.txt"); which is correct syntax. Others misuse assignment or method calls.
Final Answer:
std::ofstream file("data.txt"); -> Option A
Quick Check:
Constructor with filename opens file [OK]
Hint: Use constructor with filename to open file [OK]
Common Mistakes:
Using assignment instead of constructor
Calling open() as if it returns a value
Incorrect syntax for open method
3. What will be the content of the file output.txt after running this code?
The code tries to write to the file before opening it, which is invalid.
Step 2: Understand consequences
Writing before opening means no file is ready, so output fails silently or causes error.
Final Answer:
File is opened after writing, so output fails -> Option C
Quick Check:
Open file before writing [OK]
Hint: Always open file before writing [OK]
Common Mistakes:
Writing before opening file
Forgetting to check if file opened
Closing file before writing
5. You want to write multiple lines from a vector of strings lines to a file log.txt. Which code snippet correctly opens the file, writes all lines with newlines, and closes the file safely?
hard
A. std::ofstream file;
file.open("log.txt");
for (int i = 0; i < lines.size(); i++) file << lines[i];
file.close();
B. std::ofstream file("log.txt");
for (auto line : lines) file << line << "\n";
file.close();
C. std::ofstream file("log.txt");
for (auto line : lines) file << line;
file.close();
D. std::ofstream file;
for (auto line : lines) file << line << "\n";
file.open("log.txt");
file.close();
Solution
Step 1: Check file opening and writing order
std::ofstream file("log.txt");
for (auto line : lines) file << line << "\n";
file.close(); opens the file at construction, then writes each line with a newline, then closes the file.
Step 2: Compare other options
std::ofstream file;
file.open("log.txt");
for (int i = 0; i < lines.size(); i++) file << lines[i];
file.close(); writes without newlines; std::ofstream file("log.txt");
for (auto line : lines) file << line;
file.close(); writes without newlines; std::ofstream file;
for (auto line : lines) file << line << "\n";
file.open("log.txt");
file.close(); writes before opening file, which is wrong.
Final Answer:
std::ofstream file("log.txt");\nfor (auto line : lines) file << line << "\n";\nfile.close(); -> Option B
Quick Check:
Open file first, write lines with newlines, then close [OK]
Hint: Open file first, write lines with \n, then close [OK]
Common Mistakes:
Writing before opening file
Forgetting newlines after each line
Not closing the file
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.