Intermediate level · Module 4 of 6 · 14 guided hours
Files, Streams and Exceptions in C++
Read and write files with streams, parse CSV text with stringstreams, and handle errors with exceptions and RAII.
Learning objectives
- Read and write text files with fstream
- Parse structured text with stringstreams
- Use exceptions for errors and keep code exception-safe
Topics
- std::ifstream and std::ofstream
- checking stream state
- std::getline with delimiters
- std::istringstream parsing
- throw, try, catch
- standard exception types
- exception safety
Lessons
-
Lesson 1 Reading and Writing Files
Open files with ifstream and ofstream, check that they opened, and read and write line by line.
std::ifstreamstd::ofstreamis_open and stream statestd::filesystem::path
-
Lesson 2 Parsing CSV with String Streams
Split CSV lines into fields, convert them to numbers safely and collect malformed lines for reporting.
getline with delimiterstd::istringstreamstd::stoi / std::stodmalformed input
-
Lesson 3 Exceptions and Exception Safety
Throw and catch exceptions deliberately, use the standard exception types and write code that stays correct when exceptions occur.
throw and catch by const referencestd::runtime_errorexception safetynoexcept
Practical activity
Load a CSV of pharmacy stock, report malformed lines with line numbers and write a reorder list to a new file.
Expected outcomes
- Reliable file reading and writing
- Clear, safe error handling
Assessment
Practical: process a deliberately messy CSV file; marked on correct output, reported errors and clean resource handling.
Taught live by an instructor — online or in person. Free course lessons are for self-study and do not lead to a certificate — see registration options.