Programming Fundamentals Using C++

Lab 15 · File Handling and Exceptions

Module 15 · Lectures L29–L30 · Week 15 · 120 minutes · guided/independent 40/60

Prerequisites: completion of Labs 1–14 (Lab 1: none). Bring your laptop with the course toolchain verified (see § 3) and your exercise notebook.

2. Learning objectives

By the end of this lab you can:

These deliver the module's outcomes (PF codes in ../../LEARNING_OUTCOMES.md).

3. Required software and compiler

  g++ --version

4. Relevant theory

ifstream/ofstream, line parsing, failure taxonomy, exception demo — full treatment in the lecture notes; the lab applies it, the lecture explains it.

5. Pre-lab questions

Answer in your notebook BEFORE the session; the TA checks them at entry:

  1. Which open failure is fatal, and which data line is a skip?
  2. What does !in test after opening?
  3. When would try/catch be preferable to return codes?

6. Instructor demonstration (15 min)

The instructor or TA demonstrates: Streams, robust reading, error strategies. Watch for the workflow (how errors are read and fixed), not just the final code. The demo ends with the checkpoints you must hit in § 7.

7. Guided coding tasks

  1. guided: write-then-read-back a 5-number file
  2. guided: CSV line parsing with skip-and-report
  3. independent: sensor-log to summary-report pipeline
  4. independent: append-mode grade book with read-back report

Checkpoint rule: show each guided result to the TA before moving on. TAs give guidance, not solutions (../../TEACHING_GUIDE.md § 5).

8. Independent programming exercises

Complete the tasks labeled independent in § 7 without step-by-step help. You may consult lecture notes and this manual — not a neighbor's screen. The TA records who completes what.

9. Debugging task

Robustness clinic: 3 fault injections (missing file, bad line, empty file)

Use the five-step debug loop (reproduce, isolate, hypothesize, test, fix-and-verify) and record it in your debug log (../../labs/resources/debug_log_template.md) from Lab 4 onward. Identify the stage of each defect (compile, link, runtime, logic).

10. Test cases

Your program must pass at minimum:

Record results in the test-evidence table (../../labs/resources/submission_template.md): input, expected, actual, verdict.

11. Expected learning outcomes

After this lab, verified by the checkpoints and your submission, you can reliably read/write text files with ifstream/ofstream; use getline + istringstream for robust line parsing; check stream state and classify failures; contrast stream-state error handling with exceptions (try/catch demo). This maps to the module's PF outcomes and feeds the next lab's prerequisites.

12. Submission requirements

Submit via the course LMS before the start of next week's lab:

  1. Source file(s) named per the task list (snake_case).
  2. Test-evidence table (completed template).
  3. Debug log for the § 9 task (Lab 4 onward).
  4. Pre-lab answers (photo or scan is fine).

Code must compile warning-free under the course contract — a warning costs the compile check (§ 13).

13. Assessment rubric (10 points)

CriterionPointsEvidence
Pre-lab completed1notebook check at entry
Guided checkpoints3TA sign-offs during session
Independent tasks correct3code + test evidence
Debug task diagnosed (not just fixed)2debug log with stage + cause
Compiles warning-free + naming/format1build log, file names

Total 10, scaled to the 10 % course component. Labs 1–15 count the best 12 of 15 (drop-lowest); Lab 16 is graded as the project milestone with the same rubric plus the demo (see the instructor notes).

Programming Fundamentals Using C++ · C++17 · 16 weeksBack to top ↑