LearnFrenzy
Sign in Take a free test

File Handling (fstream) · Exercise 3 of 4

File Handling (fstream) Find the Error

  • 10 questions
  • Page 1 of 1
  • Answers explained
0Attempted
0Correct
0Wrong
Directions

Each program uses file streams incorrectly in a way the compiler rejects. Choose the option that names the real problem.

Question 1 Easy

The program below does not compile. Which statement correctly describes the problem?

main.cpp
#include <fstream>

int main() {
    std::ifstream file("notes.txt");
    file << "new note\n";
    return 0;
}
A notes.txt must exist
B ifstream is an input stream and has no operator<<; use std::ofstream (or std::fstream) to write
C The file must be opened in binary
D \n is not allowed
Question page & discussion
Question 2 Medium

The program below does not compile. Which statement correctly describes the problem?

main.cpp
#include <fstream>
#include <string>

void write_header(std::ofstream out) { out << "id,name\n"; }

int main() {
    std::ofstream file("data.csv");
    write_header(file);
    return 0;
}
A The header line is wrong
B Streams cannot be copied (their copy constructor is deleted); take std::ofstream& – or better std::ostream& so any stream works
C file must be const
D write_header must return the stream
Question page & discussion
Question 3 Easy

The program below does not compile. Which statement correctly describes the problem?

main.cpp
#include <iostream>

int main() {
    std::ofstream out("log.txt");
    out << "started\n";
    std::cout << "done\n";
    return 0;
}
A log.txt must exist
B std::ofstream is declared in <fstream>, which is not included (iostream only provides the base classes)
C out must be closed
D cout cannot be used with files
Question page & discussion
Question 4 Easy

The program below does not compile. Which statement correctly describes the problem?

main.cpp
#include <fstream>
#include <iostream>
#include <string>

int main() {
    std::ifstream in("numbers.txt");
    int value;
    while (std::getline(in, value)) std::cout << value << '\n';
    return 0;
}
A numbers.txt is missing
B std::getline reads into a std::string, not an int; read a line into a string and convert (std::stoi), or use in >> value
C value must be unsigned
D getline needs a delimiter
Question page & discussion
Question 5 Medium

The program below does not compile. Which statement correctly describes the problem?

main.cpp
#include <iostream>
#include <sstream>
#include <string>

int count_words(const std::istringstream& in) {
    int n = 0;
    std::string w;
    while (in >> w) ++n;
    return n;
}

int main() {
    std::istringstream text("a b c");
    std::cout << count_words(text) << '\n';
    return 0;
}
A istringstream needs <fstream>
B Reading changes a stream's position and state, so >> needs a non-const stream; take std::istream& (non-const)
C w must be a char
D count_words must be a template
Question page & discussion
Question 6 Medium

The program below does not compile. Which statement correctly describes the problem?

main.cpp
#include <fstream>

int main() {
    std::ofstream out("data.bin", std::ios::binary);
    int values[3] = {1, 2, 3};
    out.write(values, sizeof values);
    return 0;
}
A sizeof cannot be used on arrays
B write() takes const char*; convert explicitly with reinterpret_cast<const char*>(values)
C binary mode is not allowed with write
D values must be a vector
Question page & discussion
Question 7 Medium

The program below does not compile. Which statement correctly describes the problem?

main.cpp
#include <fstream>

int main() {
    std::ofstream log("app.log", "a");
    log << "entry\n";
    return 0;
}
A log is a reserved name
B The mode is a std::ios_base::openmode flag such as std::ios::app, not a C-style "a" string (that is fopen's convention)
C app.log must exist
D ofstream needs three arguments
Question page & discussion
Question 8 Medium

The program below does not compile. Which statement correctly describes the problem?

main.cpp
#include <filesystem>
#include <iostream>

int main() {
    std::filesystem::path dir = "reports";
    std::filesystem::path file = dir + "/jan.csv";
    std::cout << file.filename() << '\n';
    return 0;
}
A reports must be an absolute path
B std::filesystem::path has no operator+; join components with operator/ (dir / "jan.csv"), or append raw text with +=
C filename() needs an argument
D paths cannot be printed
Question page & discussion
Question 9 Medium

The program below does not compile. Which statement correctly describes the problem?

main.cpp
#include <fstream>

int main() {
    std::ofstream a("a.txt");
    std::ofstream b("b.txt");
    b = a;
    b << "hello\n";
    return 0;
}
A b must be closed first
B Streams have deleted copy assignment; to switch files call b.close() and b.open(...) (or move-assign with std::move)
C a.txt must exist
D operator<< is missing
Question page & discussion
Question 10 Hard

The program below does not compile. Which statement correctly describes the problem?

main.cpp
#include <fstream>
#include <string>

int main() {
    std::ifstream in("data.bin", std::ios::binary);
    std::string buffer(16, '\0');
    in.read(buffer, 16);
    return 0;
}
A 16 bytes is too small
B read() needs a char* destination; pass buffer.data() (non-const since C++17) – never the string object itself
C binary mode is wrong
D buffer must be a vector<int>
Question page & discussion
Up next · Exercise 4File Handling Programs10 questions

Ready to test your Programming?

Timed online tests with instant scores and explanations.

Search LearnFrenzy

Esc

Quick links