C++ File I/O Quiz
40 in-depth questions covering C++ file input/output operations, stream classes, text and binary file handling, error checking, and RAII patterns — with 16 code examples to solidify understanding.
Question 1
What is the purpose of std::ifstream in C++?
Question 2
What is the difference between text and binary file modes?
Question 3
What does the fail() function check in file streams?
#include <fstream>
#include <iostream>
std::ifstream file("data.txt");
if (file.fail()) {
std::cout << "Failed to open file" << std::endl;
}Question 4
What is RAII in the context of file I/O?
class FileHandler {
public:
FileHandler(const std::string& filename) : file_(filename) {
if (!file_.is_open()) {
throw std::runtime_error("Cannot open file");
}
}
~FileHandler() {
if (file_.is_open()) {
file_.close(); // Automatic cleanup
}
}
std::fstream& get() { return file_; }
private:
std::fstream file_;
};Question 5
What is the difference between ios::app and ios::ate file modes?
Question 6
What is the purpose of std::getline for file reading?
#include <fstream>
#include <string>
std::ifstream file("data.txt");
std::string line;
while (std::getline(file, line)) {
// Process each line
std::cout << line << std::endl;
}Question 7
What is the difference between tellg() and tellp() in file streams?
Question 8
What is the purpose of stream manipulators in file I/O?
#include <fstream>
#include <iomanip>
std::ofstream file("data.txt");
file << std::setw(10) << std::left << "Name"
<< std::setw(5) << "Age" << std::endl;
file << std::setprecision(2) << std::fixed << 3.14159;Question 9
What is the difference between ios::binary and ios::text modes?
Question 10
What is the purpose of std::fstream?
#include <fstream>
std::fstream file("data.txt", std::ios::in | std::ios::out);
if (file.is_open()) {
std::string data;
file >> data; // Read
file.seekp(0); // Move to beginning
file << "New data"; // Write
}Question 11
What is the difference between seekg() and seekp()?
Question 12
What is the purpose of std::ios_base::failure exception?
#include <fstream>
#include <ios>
std::ifstream file;
file.exceptions(std::ios::failbit | std::ios::badbit);
try {
file.open("nonexistent.txt");
} catch (const std::ios_base::failure& e) {
std::cout << "File error: " << e.what() << std::endl;
}Question 13
What is the difference between read() and operator>> for file input?
#include <fstream>
std::ifstream file("data.bin", std::ios::binary);
int value;
file.read(reinterpret_cast<char*>(&value), sizeof(value)); // Binary
std::ifstream textFile("data.txt");
textFile >> value; // Formatted textQuestion 14
What is the purpose of stream buffers in file I/O?
Question 15
What is the difference between gcount() and peek() in input streams?
#include <fstream>
std::ifstream file("data.txt");
char buffer[100];
file.read(buffer, 50);
std::streamsize bytesRead = file.gcount(); // How many bytes actually read
char nextChar = file.peek(); // Look at next character without extractingQuestion 16
What is the purpose of std::stringstream for file operations?
#include <sstream>
#include <fstream>
std::ifstream file("data.txt");
std::stringstream buffer;
buffer << file.rdbuf(); // Read entire file into stringstream
std::string content = buffer.str(); // Get as stringQuestion 17
What is the difference between file stream opening modes?
Question 18
What is the difference between formatted and unformatted I/O?
Question 19
What is the purpose of stream iterators in file I/O?
#include <fstream>
#include <iterator>
#include <vector>
std::ifstream file("numbers.txt");
std::vector<int> numbers(
std::istream_iterator<int>(file),
std::istream_iterator<int>()
); // Read all integers from fileQuestion 20
What is the difference between file position and stream position?
Question 21
What is the difference between synchronous and asynchronous file I/O?
Question 22
What is the difference between file locking and stream synchronization?
Question 23
What is the difference between file streams and string streams?
Question 24
What is the difference between buffered and unbuffered I/O?
Question 25
What is the difference between file I/O and memory-mapped I/O?
Question 26
What is the difference between file streams and C FILE* operations?
Question 27
What is the difference between stream state flags and error conditions?
Question 28
What is the difference between file opening and file stream construction?
Question 29
What is the difference between stream manipulators and stream member functions?
Question 30
What is the difference between file I/O performance and memory I/O performance?
Question 31
What is the difference between file streams and network streams?
Question 32
What is the difference between stream flushing and stream synchronization?
Question 33
What is the difference between file I/O and console I/O?
Question 34
What is the difference between stream position and file offset?
Question 35
What is the difference between file I/O and database I/O?
Question 36
What is the difference between stream inheritance and stream composition?
Question 37
What is the difference between file I/O error handling and network I/O error handling?
Question 38
What is the difference between stream formatting and data serialization?
Question 39
What is the difference between file I/O performance and stream I/O performance?
Question 40
What are the fundamental principles for effective file I/O in C++?
