0
0
C++programming~20 mins

File open and close operations in C++ - Practice Problems & Coding Challenges

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

Consider the following C++ code that opens a file, writes to it, and then closes it. What will be the output printed on the console?

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

int main() {
    std::ofstream file("test.txt");
    if (!file.is_open()) {
        std::cout << "Failed to open file" << std::endl;
        return 1;
    }
    file << "Hello, file!" << std::endl;
    file.close();
    if (file.is_open()) {
        std::cout << "File is still open" << std::endl;
    } else {
        std::cout << "File closed successfully" << std::endl;
    }
    return 0;
}
AFile closed successfully
BFailed to open file
CFile is still open
DNo output
Attempts:
2 left
πŸ’‘ Hint

Check what file.is_open() returns after closing the file.

🧠 Conceptual
intermediate
1:30remaining
Which statement about file closing is true?

In C++, what happens if you do not explicitly call close() on an ofstream object before the program ends?

AThe file remains open and data is lost
BThe program will crash due to an unclosed file
CThe file is automatically closed when the <code>ofstream</code> object is destroyed
DThe file is deleted from the disk
Attempts:
2 left
πŸ’‘ Hint

Think about what happens to objects when they go out of scope.

πŸ”§ Debug
advanced
2:30remaining
Why does this code fail to write to the file?

Examine the following code snippet. It attempts to write "Hello" to a file but the file remains empty. What is the cause?

C++
#include <fstream>

int main() {
    std::ofstream file;
    file << "Hello";
    file.open("output.txt");
    file.close();
    return 0;
}
AThe file is closed before opening
BThe file is not opened at all
CThe file is opened in read mode only
DThe file is opened after writing, so data is not saved
Attempts:
2 left
πŸ’‘ Hint

Check the order of operations: when is the file opened vs when is data written?

πŸ“ Syntax
advanced
2:00remaining
Which option will cause a compilation error?

Which of the following code snippets will cause a compilation error when trying to open a file for writing?

Astd::ofstream file("data.txt");
Bstd::ifstream file("data.txt"); file << "Hello";
Cstd::ofstream file; file.open("data.txt");
Dstd::ofstream file; file.open("data.txt", std::ios::out);
Attempts:
2 left
πŸ’‘ Hint

Consider which stream type supports writing with the << operator.

πŸš€ Application
expert
3:00remaining
How many times is the file opened in this code?

Analyze the following code. How many times is the file "log.txt" opened during execution?

C++
#include <fstream>

void writeLog() {
    std::ofstream file("log.txt", std::ios::app);
    file << "Log entry\n";
}

int main() {
    for (int i = 0; i < 3; ++i) {
        writeLog();
    }
    return 0;
}
A3
B0
C1
D6
Attempts:
2 left
πŸ’‘ Hint

Consider how many times the function writeLog() is called and what happens inside it.