Unit 8: Files and Exceptions
Overview
This unit covers read/write text, resource lifetime, error handling, JSON serialization and persistence. Follow the same problem progression as Python so that the main comparison is language syntax and semantics. Use the worked examples as starting points, then complete the activities independently.
Lesson 8.1: Reading text files
Reading References
- Official C++ language guide and standard-library documentation.
- Corresponding Python course lesson for the shared problem context and learning objective.
Learning Objectives
- Explain this concept using C++ terminology.
- Trace a short program and predict its behavior before execution.
- Modify and test the example independently.
Concept Explanation
Reading text files is taught through the shared course datasets and requirements. The Python curriculum supplies a common problem context, not syntax to translate mechanically. In C++, make the type system, object lifetime, standard-library contracts, ownership, and failure behavior explicit. This lesson focuses on: check file-open state; remember getline removes the delimiter; resolve relative paths from the process working directory.
Worked Example
{ std::ofstream out("cpp_notes.txt"); out << "First line\nSecond line\n"; } std::ifstream in("cpp_notes.txt"); std::string line; while (std::getline(in, line)) std::cout << line << '\n';
Trace: predict the output before compiling. Identify declarations, values, control-flow decisions, and state changes in order. Then compile and compare actual output with the prediction. The lesson example is intended to be independently compilable.
Expected Behavior or Output
Output observed when the worked example was run with empty standard input (g++ 13.3, C++20; the text of the output does not depend on the operating system):
First line
Second line
Common Mistakes
- Check file-open state.
- Remember getline removes the delimiter.
- Resolve relative paths from the process working directory.
Activities
- Trace the reading text files example and predict its output before compiling.
- Adapt the example to notes files and personal-data persistence and preserve the same observable requirement using idiomatic C++20.
- Add one boundary case and one invalid or missing-data case; state the expected result and implement the handling.
- Write a short test or trace table, then explain the C++ type, lifetime, ownership, iterator, or error-handling rule that matters here.
Accessibility Practice
Navigate the complete source file by line, word, and character using the learner’s access technology. Read punctuation and indentation, run from the terminal, and inspect standard output and errors sequentially.
Skills Checklist
- I can explain the concept in C++ terminology.
- I can trace and predict the example.
- I can modify and run the code independently.
- I can identify and correct a common error.
- I can read source and output accessibly.
Lesson 8.2: Writing and appending files
Reading References
- Official C++ language guide and standard-library documentation.
- Corresponding Python course lesson for the shared problem context and learning objective.
Learning Objectives
- Explain this concept using C++ terminology.
- Trace a short program and predict its behavior before execution.
- Modify and test the example independently.
Concept Explanation
Writing and appending files is taught through the shared course datasets and requirements. The Python curriculum supplies a common problem context, not syntax to translate mechanically. In C++, make the type system, object lifetime, standard-library contracts, ownership, and failure behavior explicit. This lesson focuses on: choose truncation or append mode deliberately; flush/close before dependent reads; check write state.
Worked Example
{ std::ofstream out("cpp_log.txt", std::ios::app); out << "Session complete\n"; } std::cout << "Appended one record\n";
Trace: predict the output before compiling. Identify declarations, values, control-flow decisions, and state changes in order. Then compile and compare actual output with the prediction. The lesson example is intended to be independently compilable.
Expected Behavior or Output
Output observed when the worked example was run with empty standard input (g++ 13.3, C++20; the text of the output does not depend on the operating system):
Appended one record
Common Mistakes
- Choose truncation or append mode deliberately.
- Flush/close before dependent reads.
- Check write state.
Activities
- Trace the writing and appending files example and predict its output before compiling.
- Adapt the example to notes files and personal-data persistence and preserve the same observable requirement using idiomatic C++20.
- Add one boundary case and one invalid or missing-data case; state the expected result and implement the handling.
- Write a short test or trace table, then explain the C++ type, lifetime, ownership, iterator, or error-handling rule that matters here.
Accessibility Practice
Navigate the complete source file by line, word, and character using the learner’s access technology. Read punctuation and indentation, run from the terminal, and inspect standard output and errors sequentially.
Skills Checklist
- I can explain the concept in C++ terminology.
- I can trace and predict the example.
- I can modify and run the code independently.
- I can identify and correct a common error.
- I can read source and output accessibly.
Lesson 8.3: Errors and exceptions
Reading References
- Official C++ language guide and standard-library documentation.
- Corresponding Python course lesson for the shared problem context and learning objective.
Learning Objectives
- Explain this concept using C++ terminology.
- Trace a short program and predict its behavior before execution.
- Modify and test the example independently.
Concept Explanation
Errors and exceptions is taught through the shared course datasets and requirements. The Python curriculum supplies a common problem context, not syntax to translate mechanically. In C++, make the type system, object lifetime, standard-library contracts, ownership, and failure behavior explicit. This lesson focuses on: catch standard exceptions by const reference; handle expected input errors directly; avoid uncaught exceptions.
Worked Example
try { throw std::runtime_error("invalid score"); } catch (const std::exception& error) { std::cout << "Handled: " << error.what() << '\n'; }
Trace: predict the output before compiling. Identify declarations, values, control-flow decisions, and state changes in order. Then compile and compare actual output with the prediction. The lesson example is intended to be independently compilable.
Expected Behavior or Output
Output observed when the worked example was run with empty standard input (g++ 13.3, C++20; the text of the output does not depend on the operating system):
Handled: invalid score
Common Mistakes
- Catch standard exceptions by const reference.
- Handle expected input errors directly.
- Avoid uncaught exceptions.
Activities
- Trace the errors and exceptions example and predict its output before compiling.
- Adapt the example to notes files and personal-data persistence and preserve the same observable requirement using idiomatic C++20.
- Add one boundary case and one invalid or missing-data case; state the expected result and implement the handling.
- Write a short test or trace table, then explain the C++ type, lifetime, ownership, iterator, or error-handling rule that matters here.
Accessibility Practice
Navigate the complete source file by line, word, and character using the learner’s access technology. Read punctuation and indentation, run from the terminal, and inspect standard output and errors sequentially.
Skills Checklist
- I can explain the concept in C++ terminology.
- I can trace and predict the example.
- I can modify and run the code independently.
- I can identify and correct a common error.
- I can read source and output accessibly.
Lesson 8.4: Resource cleanup
Reading References
- Official C++ language guide and standard-library documentation.
- Corresponding Python course lesson for the shared problem context and learning objective.
Learning Objectives
- Explain this concept using C++ terminology.
- Trace a short program and predict its behavior before execution.
- Modify and test the example independently.
Concept Explanation
Resource cleanup is taught through the shared course datasets and requirements. The Python curriculum supplies a common problem context, not syntax to translate mechanically. In C++, make the type system, object lifetime, standard-library contracts, ownership, and failure behavior explicit. This lesson focuses on: prefer RAII to manual cleanup; check stream state after writes; do not copy unique resource owners.
Worked Example
{ std::ofstream output("cpp_raii.txt"); output << "The stream closes automatically at scope exit\n"; } std::cout << "RAII released the file stream\n";
Trace: predict the output before compiling. Identify declarations, values, control-flow decisions, and state changes in order. Then compile and compare actual output with the prediction. The lesson example is intended to be independently compilable.
Expected Behavior or Output
Output observed when the worked example was run with empty standard input (g++ 13.3, C++20; the text of the output does not depend on the operating system):
RAII released the file stream
Common Mistakes
- Prefer RAII to manual cleanup.
- Check stream state after writes.
- Do not copy unique resource owners.
Activities
- Trace the resource cleanup example and predict its output before compiling.
- Adapt the example to notes files and personal-data persistence and preserve the same observable requirement using idiomatic C++20.
- Add one boundary case and one invalid or missing-data case; state the expected result and implement the handling.
- Write a short test or trace table, then explain the C++ type, lifetime, ownership, iterator, or error-handling rule that matters here.
Accessibility Practice
Navigate the complete source file by line, word, and character using the learner’s access technology. Read punctuation and indentation, run from the terminal, and inspect standard output and errors sequentially.
Skills Checklist
- I can explain the concept in C++ terminology.
- I can trace and predict the example.
- I can modify and run the code independently.
- I can identify and correct a common error.
- I can read source and output accessibly.
Lesson 8.5: JSON persistence
Reading References
- Official C++ language guide and standard-library documentation.
- Corresponding Python course lesson for the shared problem context and learning objective.
Learning Objectives
- Explain this concept using C++ terminology.
- Trace a short program and predict its behavior before execution.
- Modify and test the example independently.
Concept Explanation
JSON persistence is taught through the shared course datasets and requirements. The Python curriculum supplies a common problem context, not syntax to translate mechanically. In C++, make the type system, object lifetime, standard-library contracts, ownership, and failure behavior explicit. This lesson focuses on: distinguish JSON serialization from parsing; escape special characters; use a documented JSON library for general data.
Worked Example
const std::string json = "{\"name\":\"Avery\",\"score\":92}"; std::cout << json << '\n';
Trace: predict the output before compiling. Identify declarations, values, control-flow decisions, and state changes in order. Then compile and compare actual output with the prediction. The lesson example is intended to be independently compilable.
Expected Behavior or Output
Output observed when the worked example was run with empty standard input (g++ 13.3, C++20; the text of the output does not depend on the operating system):
{"name":"Avery","score":92}
Common Mistakes
- Distinguish JSON serialization from parsing.
- Escape special characters.
- Use a documented JSON library for general data.
Activities
- Trace the json persistence example and predict its output before compiling.
- Adapt the example to notes files and personal-data persistence and preserve the same observable requirement using idiomatic C++20.
- Add one boundary case and one invalid or missing-data case; state the expected result and implement the handling.
- Write a short test or trace table, then explain the C++ type, lifetime, ownership, iterator, or error-handling rule that matters here.
Accessibility Practice
Navigate the complete source file by line, word, and character using the learner’s access technology. Read punctuation and indentation, run from the terminal, and inspect standard output and errors sequentially.
Skills Checklist
- I can explain json persistence using correct C++ terminology and distinguish it from the closest Python equivalent.
- I can trace the example and justify its output using C++ rules.
- I can implement the same requirement for notes files and personal-data persistence without copying Python syntax.
- I can test a normal case, a boundary case, and a relevant invalid or failure case.
- I can compile the program, interpret diagnostics, and navigate source and terminal output using my access technology.