Skills101
← All practice quizzes

Quiz: File Handling in C++

5 questions · new set on every retakeNo time limitPass: 60%+10 XP on first pass

Save data to files and read it back with C++ file streams. Writing with ofstream, reading lines and fields with ifstream, checking for errors, append mode, the file mode flags, and binary files with read, write, seekg and tellg.

Log in to take the quiz

Free account · see your score, answers and explanations, earn XP

  1. Question 1

    Which class do you use to read data from a file?

  2. Question 2

    What happens when you open an existing file with ofstream out("data.txt");?

  3. Question 3

    Which loop reads every line of a file correctly?

  4. Question 4

    What does this print?

    #include <fstream>
    #include <iostream>
    using namespace std;
    int main() {
        { ofstream f("t.txt"); f << "10 20\n30"; }
        { ofstream f("t.txt", ios::app); f << " 40"; }
        ifstream in("t.txt");
        int x, sum = 0, n = 0;
        while (in >> x) { sum += x; n++; }
        cout << n << " " << sum << "\n";
        return 0;
    }
    
  5. Question 5

    Why is while (!in.eof()) a bad way to read records?

🎯 Today’s placement challenge — 10 questions, no login. Can you score 10/10?Take today’s challenge →