0
0
C++programming~20 mins

File input using ifstream in C++ - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
πŸŽ–οΈ
File Input Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Reading integers from a file
What is the output of this C++ program if the file numbers.txt contains the numbers 10 20 30?
C++
#include <iostream>
#include <fstream>
using namespace std;

int main() {
    ifstream file("numbers.txt");
    int sum = 0, x;
    while (file >> x) {
        sum += x;
    }
    cout << sum << endl;
    return 0;
}
A0
B102030
C10 20 30
D60
Attempts:
2 left
πŸ’‘ Hint
The program reads integers one by one and adds them.
❓ Predict Output
intermediate
2:00remaining
Reading lines from a file
What will this program print if data.txt contains:
Hello
World
C++
C++
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main() {
    ifstream file("data.txt");
    string line;
    int count = 0;
    while (getline(file, line)) {
        count++;
    }
    cout << count << endl;
    return 0;
}
A3
BHello World C++
C0
D1
Attempts:
2 left
πŸ’‘ Hint
Count how many lines the file has.
πŸ”§ Debug
advanced
2:00remaining
Why does this file reading loop fail?
This code tries to read integers from input.txt and print them. Why does it print the last number twice?
C++
#include <iostream>
#include <fstream>
using namespace std;

int main() {
    ifstream file("input.txt");
    int x;
    while (!file.eof()) {
        file >> x;
        cout << x << endl;
    }
    return 0;
}
ABecause cout prints the buffer twice
BBecause checking eof() before reading causes the last read to fail but still prints the old value
CBecause the variable x is not initialized
DBecause the file is empty
Attempts:
2 left
πŸ’‘ Hint
Check how eof() works in file streams.
πŸ“ Syntax
advanced
2:00remaining
Identify the syntax error in file reading
Which option contains the correct syntax to open a file and read a single integer?
Aifstream file; file.open = "data.txt"; int x; file >> x;
Bifstream file = "data.txt"; int x; file >> x;
Cifstream file("data.txt"); int x; file >> x;
Difstream file("data.txt"); int x; file.read(x);
Attempts:
2 left
πŸ’‘ Hint
Check how ifstream is constructed and how to read data.
πŸš€ Application
expert
3:00remaining
Count words in a file
Which program correctly counts the number of words in text.txt?
A
#include &lt;fstream&gt;
#include &lt;string&gt;
#include &lt;iostream&gt;
using namespace std;
int main() {
    ifstream file("text.txt");
    string word;
    int count = 0;
    while (file &gt;&gt; word) {
        count++;
    }
    cout &lt;&lt; count &lt;&lt; endl;
    return 0;
}
B
#include &lt;fstream&gt;
#include &lt;string&gt;
#include &lt;iostream&gt;
using namespace std;
int main() {
    ifstream file("text.txt");
    string line;
    int count = 0;
    while (getline(file, line)) {
        count += line.size();
    }
    cout &lt;&lt; count &lt;&lt; endl;
    return 0;
}
C
#include &lt;fstream&gt;
#include &lt;string&gt;
#include &lt;iostream&gt;
using namespace std;
int main() {
    ifstream file("text.txt");
    char c;
    int count = 0;
    while (file.get(c)) {
        if (c == ' ') count++;
    }
    cout &lt;&lt; count &lt;&lt; endl;
    return 0;
}
D
#include &lt;fstream&gt;
#include &lt;string&gt;
#include &lt;iostream&gt;
using namespace std;
int main() {
    ifstream file("text.txt");
    int count = 0;
    while (!file.eof()) {
        string word;
        file &gt;&gt; word;
        count++;
    }
    cout &lt;&lt; count &lt;&lt; endl;
    return 0;
}
Attempts:
2 left
πŸ’‘ Hint
Words are separated by whitespace and can be read with >> operator.