0
0
C++programming~20 mins

File output using ofstream in C++ - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
πŸŽ–οΈ
File Output Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the output in the file after running this code?

Consider the following C++ code that writes to a file using ofstream. What will be the content of the file output.txt after running this program?

C++
#include <fstream>
#include <string>

int main() {
    std::ofstream file("output.txt");
    file << "Hello" << std::endl;
    file << "World" << std::endl;
    file.close();
    return 0;
}
A"Hello\nWorld\n"
B"HelloWorld"
C"Hello World"
D"Hello\nWorld"
Attempts:
2 left
πŸ’‘ Hint

Remember that std::endl inserts a newline character and flushes the stream.

❓ Predict Output
intermediate
2:00remaining
What happens if the file cannot be opened?

What will be the output of this program if the file /root/secret.txt cannot be opened due to permission issues?

C++
#include <fstream>
#include <iostream>

int main() {
    std::ofstream file("/root/secret.txt");
    if (!file) {
        std::cout << "Failed to open file" << std::endl;
    } else {
        file << "Data" << std::endl;
    }
    return 0;
}
AFailed to open file
BData
CCompilation error
DNo output
Attempts:
2 left
πŸ’‘ Hint

Check how the program tests if the file stream is valid.

πŸ”§ Debug
advanced
2:00remaining
Why does this code not write anything to the file?

The following code is supposed to write "Test" to log.txt, but the file remains empty. What is the reason?

C++
#include <fstream>

int main() {
    std::ofstream file;
    file << "Test";
    file.close();
    return 0;
}
AThe program lacks write permissions
BThe file stream was not closed properly
CThe file was never opened before writing
DThe file path is incorrect
Attempts:
2 left
πŸ’‘ Hint

Check how the ofstream object is created and used.

🧠 Conceptual
advanced
2:00remaining
What is the effect of opening a file with std::ofstream in append mode?

What happens when you open a file with std::ofstream file("data.txt", std::ios::app); and write data to it?

AThe existing file content is erased before writing
BThe file is locked and cannot be written
CThe file is opened in read-only mode
DNew data is added at the end of the existing file content
Attempts:
2 left
πŸ’‘ Hint

Think about what append mode means for files.

πŸ“ Syntax
expert
3:00remaining
Which option correctly opens a file for writing and checks for errors?

Choose the code snippet that correctly opens a file named report.txt for writing using ofstream and checks if the file was opened successfully.

A
#include &lt;fstream&gt;

int main() {
    std::ofstream file("report.txt");
    if (!file.is_open()) {
        return 1;
    }
    file &lt;&lt; "Report data";
    file.close();
    return 0;
}
B
#include &lt;fstream&gt;

int main() {
    std::ofstream file;
    file.open("report.txt");
    if (!file) {
        return 1;
    }
    file &lt;&lt; "Report data";
    file.close();
    return 0;
}
C
#include &lt;fstream&gt;

int main() {
    std::ofstream file;
    file.open("report.txt");
    if (file == nullptr) {
        return 1;
    }
    file &lt;&lt; "Report data";
    file.close();
    return 0;
}
D
#include &lt;fstream&gt;

int main() {
    std::ofstream file("report.txt");
    if (file == NULL) {
        return 1;
    }
    file &lt;&lt; "Report data";
    file.close();
    return 0;
}
Attempts:
2 left
πŸ’‘ Hint

Check how to test if an ofstream is open and valid.