The program below does not compile. Which statement correctly describes the problem?
#include <fstream>
int main() {
std::ifstream file("notes.txt");
file << "new note\n";
return 0;
}
ifstream derives from istream only.
File Handling (fstream) · Exercise 3 of 4
Each program uses file streams incorrectly in a way the compiler rejects. Choose the option that names the real problem.
The program below does not compile. Which statement correctly describes the problem?
#include <fstream>
int main() {
std::ifstream file("notes.txt");
file << "new note\n";
return 0;
}
ifstream derives from istream only.
The program below does not compile. Which statement correctly describes the problem?
#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;
}
Streams are move-only; pass them by reference.
The program below does not compile. Which statement correctly describes the problem?
#include <iostream>
int main() {
std::ofstream out("log.txt");
out << "started\n";
std::cout << "done\n";
return 0;
}
Some libraries happen to include it indirectly, but you must not rely on that.
The program below does not compile. Which statement correctly describes the problem?
#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;
}
Overloads exist for std::string (and char arrays via in.getline).
The program below does not compile. Which statement correctly describes the problem?
#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;
}
Taking std::istream& also lets the function read files, strings or std::cin.
The program below does not compile. Which statement correctly describes the problem?
#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;
}
The cast makes the "raw bytes" intent visible.
The program below does not compile. Which statement correctly describes the problem?
#include <fstream>
int main() {
std::ofstream log("app.log", "a");
log << "entry\n";
return 0;
}
Write std::ofstream log("app.log", std::ios::app);
The program below does not compile. Which statement correctly describes the problem?
#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;
}
operator/ inserts the correct separator for the platform; += appends characters without adding one.
The program below does not compile. Which statement correctly describes the problem?
#include <fstream>
int main() {
std::ofstream a("a.txt");
std::ofstream b("b.txt");
b = a;
b << "hello\n";
return 0;
}
A stream owns an OS file handle, which cannot be duplicated by copying.
The program below does not compile. Which statement correctly describes the problem?
#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;
}
The string must already have the right size, as here.
Timed online tests with instant scores and explanations.