Last Updated: December 6, 2025
31 quizzes
Which header must you include to use std::ifstream and std::ofstream?
What is the safest way to open a file for reading text?
Which open mode will append to an existing text file without truncating it?
Which is the most memory‑efficient way to process a huge log file?
Which statement correctly opens a binary file for both reading and writing?
Which function retrieves the current read position in an ifstream?
What is a practical use of std::istringstream?
Why is std::getline often preferred over operator>> for reading config lines?
Which approach best preserves class invariants when saving an object to a text file?
When writing portable binary files containing integers, what is MOST important?
Which is a good pattern to ensure a file handle is always closed?
Open a text file for reading using std::ifstream and check success.
std::ifstream in("config.txt"); if (!in.()) { throw std::runtime_error("open failed"); }Click an option to fill the blank:
Write a POD struct to a binary file using std::ofstream::write.
out.write(reinterpret_cast<char*>(&person), (person));Click an option to fill the blank:
Construct a string stream for parsing a log line.
std::istringstream iss();Click an option to fill the blank:
What is the output of this code reading line by line?
1#include <iostream>
2#include <fstream>
3#include <string>
4int main() {
5 std::istringstream file("one\ntwo\nthree\n");
6 std::string s;
7 int count = 0;
8 while (std::getline(file, s)) {
9 if (!s.empty()) ++count;
10 }
11 std::cout << count << "\n";
12 return 0;
13}What does the program print about the file size and position?
1#include <iostream>
2#include <fstream>
3int main() {
4 std::istringstream in("abcd");
5 in.seekg(0, std::ios::end);
6 auto size = in.tellg();
7 in.seekg(1, std::ios::beg);
8 auto pos = in.tellg();
9 std::cout << size << " " << pos << "\n";
10 return 0;
11}What is printed after writing and rereading from a stringstream?
1#include <iostream>
2#include <sstream>
3int main() {
4 std::stringstream ss;
5 ss << 10 << " " << 20;
6 int a = 0, b = 0;
7 ss >> a;
8 ss >> b;
9 std::cout << a + b << "\n";
10 return 0;
11}What will this binary write/read example output?
1#include <iostream>
2#include <sstream>
3#include <vector>
4int main() {
5 std::stringstream buf(std::ios::in | std::ios::out | std::ios::binary);
6 std::vector<int> v{1,2,3};
7 buf.write(reinterpret_cast<char*>(v.data()), v.size() * sizeof(int));
8 buf.seekg(0);
9 int x[3] = {0,0,0};
10 buf.read(reinterpret_cast<char*>(x), 3 * sizeof(int));
11 std::cout << x[0] << x[1] << x[2] << "\n";
12 return 0;
13}What is the output when parsing with std::istringstream?
1#include <iostream>
2#include <sstream>
3#include <string>
4int main() {
5 std::string data = "42 cpp 3.5";
6 std::istringstream iss(data);
7 int i; std::string s; double d;
8 iss >> i >> s >> d;
9 std::cout << i << ":" << s << ":" << d << "\n";
10 return 0;
11}Find the bug in this file reading code
Click on the line(s) that contain the bug.
#include <fstream>#include <string>void load(const std::string& path) { std::ifstream in(path); std::string line; while (!in.eof()) { std::getline(in, line); // process line }}Find the bug in this binary serialization code
Click on the line(s) that contain the bug.
#include <fstream>struct Record { int id; double value;};void save(const Record& r) { std::ofstream out("rec.dat", std::ios::binary); out.write(reinterpret_cast<const char*>(&r), sizeof(&r));} Click the line that risks losing buffered data when writing to a file
Click on the line to select.
#include <fstream>#include <string>void writeLog(const std::string& msg) { std::ofstream log("log.txt"); log << msg; if (!log) return; // check error} Click the line that moves the file pointer to the end of the file
Click on the line to select.
#include <fstream>void inspect() { std::ifstream in("data.txt", std::ios::binary); in.seekg(0, std::ios::end); auto size = in.tellg();} Complete the loop that reads a text file line by line and prints it.
#include <fstream>#include <string>#include <iostream>int main() { std::ifstream in("data.txt"); std::string line; while ((in, line)) { std::cout << << '\n'; }}Click an option to fill blank 1:
Fill in code to write binary data and then seek back to the beginning.
#include <fstream>int main() { int value = 123; std::ofstream out("num.bin", std::ios:: | std::ios::); out.write(reinterpret_cast<char*>(&value), sizeof(value)); out.(0, std::ios::beg);}Click an option to fill blank 1:
Complete this parsing code using std::istringstream.
#include <sstream>#include <string>#include <iostream>int main() { std::string data = "10 20"; std:: iss(data); int a = 0, b = 0; iss >> a >> b; std::cout << << '\n';}Click an option to fill blank 1:
Complete the code that positions within a file to read its last character.
#include <fstream>#include <iostream>int main() { std::ifstream in("text.txt", std::ios::in); in.seekg(0, std::ios::); auto size = in.(); in.seekg(size - 1, std::ios::beg); char c; in.get(c); std::cout << c << '\n';}Click an option to fill blank 1:
Match each stream type with its primary use in file/string I/O.
Click an item on the left, then click its match on the right. Click a matched item to unmatch.
Match each flag with what it controls when opening file streams.
Click an item on the left, then click its match on the right. Click a matched item to unmatch.
Order the typical steps to safely read configuration from a text file.
Drag and drop to reorder, or use the arrows.
Order the steps for writing a POD object to and then reading it from a binary file.
Drag and drop to reorder, or use the arrows.