Understand the purpose of storing data in a file

📁 Programming: Storing Data in a File

Why Store Data?

Imagine you’re a librarian. The books (your data) need to be kept somewhere safe so you can find them later. Storing data in a file is like putting those books in a well‑labelled shelf that stays even after you close the computer. It lets you save, retrieve, and share information across different programs and sessions.

Common File Types

  • 📄 Text files (.txt) – plain characters, easy to read.
  • 📊 CSV (.csv) – comma‑separated values, great for tables.
  • 🖼️ Images (.png, .jpg) – binary data for pictures.
  • 📦 Binary files (.bin) – raw bytes, used for complex data.

Reading & Writing Files

Think of a file as a diary. Writing is like adding a new entry, and reading is flipping back to see what you wrote.

  1. Open the file in the desired mode (read, write, append).
  2. Perform the operation (write data, read data).
  3. Close the file to free resources.

Example (pseudo‑code):

open("scores.txt", "w")   // write mode
write("Alice, 88")
write("Bob, 76")
close()
  

File Formats & Structure

Format Typical Use Example
.txt Simple text notes notes.txt
.csv Spreadsheet data students.csv
.json Structured data for web config.json

Exam Tips 📚

  • Know the difference between text and binary files.
  • Remember the three main file modes: r (read), w (write), a (append).
  • Practice writing a small program that saves and loads a list of student names.
  • Use the analogy of a diary or library to explain why persistence matters.
  • Be ready to explain why you would choose CSV over TXT for tabular data.

Revision

Log in to practice.

0 views 0 suggestions