Code Labs: File Handling

Master opening, appending, and safely closing persistent files.

Interactive Engine

Use the interactive terminal below to test syntax.

> Waiting to compile...

The Crucible

Prove your knowledge of file operations.

Bronze (File Integrity)

The Memory Trap

A critical step in file handling is often forgotten, causing data loss and memory leaks.

Task: Read the pseudocode simulation below. The programmer opened the file and wrote to it, but forgot the final crucial command. Add the .close() method to seal the logFile safely.

> Output will appear here...
Silver (Syntax)

Erase & Write

When opening a file, you must specify the mode. If you want to overwrite all existing data with something new, you use Write Mode.

Task: Create a variable called mode and set it to the correct single character string for Write mode.

> Output will appear here...
Gold (Advanced)

Adding to the Bottom

Write mode completely wipes out existing data. To add to the bottom of the file safely, we use Append Mode.

Task: Read the code. Replace the ??? to correctly use Append Mode so the previous scores are not deleted.

> Output will appear here...