0
0
C++programming~20 mins

Reading and writing files 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 reading code?

Consider the following C++ code that reads from a file named data.txt containing the text "Hello World". What will be printed to the console?

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

int main() {
    std::ifstream file("data.txt");
    std::string word;
    while (file >> word) {
        std::cout << word << " ";
    }
    return 0;
}
AHello\nWorld\n
BHelloWorld
CHello World
D
Hello
World
Attempts:
2 left
πŸ’‘ Hint

Think about how the extraction operator >> reads words separated by whitespace.

❓ Predict Output
intermediate
2:00remaining
What does this file writing code produce in the file?

Given the following C++ code, what will be the content of the file output.txt after running it?

C++
#include <fstream>

int main() {
    std::ofstream file("output.txt");
    file << "Line1\n";
    file << "Line2";
    file.close();
    return 0;
}
ALine1\nLine2
B
Line1
Line2
CLine1 Line2
DLine1\n\nLine2
Attempts:
2 left
πŸ’‘ Hint

Remember that \n is a newline character in C++ strings.

πŸ”§ Debug
advanced
2:00remaining
Why does this file reading code fail to read the entire line?

Look at this C++ code snippet intended to read a full line from a file. Why does it only read the first word?

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

int main() {
    std::ifstream file("input.txt");
    std::string line;
    file >> line;
    std::cout << line << std::endl;
    return 0;
}
ABecause operator >> reads only until the first whitespace, not the whole line.
BBecause the file is not opened in binary mode.
CBecause the string variable is not initialized.
DBecause the file stream is not flushed before reading.
Attempts:
2 left
πŸ’‘ Hint

Think about the difference between operator>> and getline().

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

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

A
#include &lt;fstream&gt;
int main() {
    std::ofstream file("test.txt");
    file &lt;&lt; "Hello";
    return 0;
}
B
#include &lt;fstream&gt;
int main() {
    std::ofstream file;
    file.open("test.txt");
    file &lt;&lt; "Hello";
    return 0;
}
C
#include &lt;fstream&gt;
int main() {
    std::ofstream file("test.txt");
    file.write("Hello", 5);
    return 0;
}
D
#include &lt;fstream&gt;
int main() {
    std::ofstream file;
    file.open();
    file &lt;&lt; "Hello";
    return 0;
}
Attempts:
2 left
πŸ’‘ Hint

Check the parameters required by open() method.

πŸš€ Application
expert
2:00remaining
How many lines will be in the output file after running this code?

This C++ program reads lines from input.txt and writes only non-empty lines to output.txt. If input.txt contains 5 lines, where 2 lines are empty, how many lines will output.txt have?

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

int main() {
    std::ifstream infile("input.txt");
    std::ofstream outfile("output.txt");
    std::string line;
    while (std::getline(infile, line)) {
        if (!line.empty()) {
            outfile << line << "\n";
        }
    }
    return 0;
}
A3
B0
C2
D5
Attempts:
2 left
πŸ’‘ Hint

Count only lines that are not empty.