ベータこのプラットフォームは活発に開発中です。バグ、未実装の機能、データ消失のリスクがあります。応援ありがとうございます!

File Handling

遊んで学ぼう

問題に答えてエネルギーを集めたら、釣りや探検を楽しもう。アカウント不要。

先生の方へ: File Handling(Computer Science、CIE)向けのすぐ使えるレッスンスライド, 復習ノート — レッスンで使うか、生徒がライブゲームとして遊ぶインタラクティブなクラス活動としてトピックを実施できます。

レッスンノート

What is File Handling?

  • File handling uses programming techniques to work with information stored in text files.
  • Common operations: open, read, write, and close files.
  • Files provide permanent storage for data beyond program execution.

Opening Files

  • Use `OPENFILE "filename" FOR READ` (pseudocode) or `open("filename", "r")` (Python) to open for reading.
  • Use `OPENFILE "filename" FOR WRITE` or `open("filename", "w")` to open for writing – creates new file or overwrites existing.
  • Use `OPENFILE "filename" FOR APPEND` or `open("filename", "a")` to open for appending – adds data to end of existing file.
  • Always close files after use with `CLOSEFILE` or `file.close()`.

Reading from Files

  • `READFILE "file", variable` (pseudocode) or `file.readline()` (Python) reads one line as a string.
  • Use a loop with an end-of-file flag to read all records.
  • `TRIM()` (pseudocode) or `.strip()` (Python) removes extra spaces/newlines from read data.
  • Check for empty string to detect end of file.

Writing to Files

  • `WRITEFILE "file", data` (pseudocode) or `file.write(data)` (Python) writes a string to the file.
  • Add `NEWLINE` (pseudocode) or `"\n"` (Python) to separate records/lines.
  • Overwrite mode (`"w"`) deletes existing content; append mode (`"a"`) preserves it.

Pseudocode Example (Reading)

  • Open file for read, set `endOfFile ← FALSE`.
  • Loop: read name, department, salary, age; trim each.
  • If name is empty, set `endOfFile ← TRUE`; else output details.
  • Close file after loop.

Python Example (Reading)

  • `file = open("employees.txt", "r")` then loop using `readline().strip()`.
  • Check `if name == "":` to set `endOfFile = True`.
  • Print fields and close file with `file.close()`.

Pseudocode Example (Writing/Appending)

  • Open file for append: `OPENFILE "employees.txt" FOR APPEND`.
  • Write each field on its own line, adding `NEWLINE` after each.
  • Close file when done.

Python Example (Writing/Appending)

  • `file = open("employees.txt", "a")` then `file.write("Polly\n")` etc.
  • Close file with `file.close()`.

Examiner Tips & Common Mistakes

  • Use correct mode letter: `"r"` for read, `"w"` for write (overwrites), `"a"` for append.
  • Always close files to avoid data loss or corruption.
  • Backup text files before testing – one mistake can lose contents.

Worked Example (CIE Style)

  • Input title and year from user.
  • Open `books.txt` for write (or append if preserving existing data).
  • Write title and year to file, then close.
  • Marks: input (1), open (1), write (1), close (1).

スライド

Sign up free to view the lesson slides

Step through every slide for this topic — plus flashcards and revision notes — with a free account.

練習問題

無料プレビュー — 57問中8問。すべて見るには登録を。
  1. 1.What does the 'r' mode in file handling allow you to do?

    Easy
    • ARead from a file only
    • BWrite to a new file
    • CAppend to an existing file
    • DRead and write to a file
  2. 2.Which file mode would you use to add data to the end of an existing file without overwriting it?

    Easy
    • A'w'
    • B'a'
    • C'r'
    • D'x'
  3. 3.What is the purpose of the CLOSEFILE command in pseudocode?

    Easy
    • ATo delete the file from the disk
    • BTo release the file from the program's control
    • CTo clear all data in the file
    • DTo open the file for reading
  4. 4.A program uses the pseudocode statement: OPENFILE "data.txt" FOR WRITE. What happens if "data.txt" already exists?

    Easy
    • AThe program appends data to the end of the file
    • BThe program overwrites the existing contents of the file
    • CThe program raises an error and stops
    • DThe program reads the existing contents first
  5. 5.In the following pseudocode, what is the purpose of the variable endOfFile?

    Easy
    • ATo count the number of lines read
    • BTo signal when there is no more data to read
    • CTo store the last line of the file
    • DTo indicate the size of the file
  6. 6.A text file contains employee records with each record spread over four lines: name, department, salary, age. Which pseudocode correctly reads one record?

    Medium
    • AREADFILE "employees.txt", name READFILE "employees.txt", department READFILE "employees.txt", salary READFILE "employees.txt", age
    • BREADFILE "employees.txt", name, department, salary, age
    • COPENFILE "employees.txt" FOR READ READFILE "employees.txt", name, department, salary, age
    • DREADFILE "employees.txt", record SPLIT record INTO name, department, salary, age
  7. 7.Consider the pseudocode: OPENFILE "data.txt" FOR READ WHILE NOT EOF READFILE "data.txt", line IF line = "" THEN BREAK ENDIF OUTPUT line ENDWHILE CLOSEFILE "data.txt" What is the purpose of the IF statement checking for an empty line?

    Medium
    • ATo skip blank lines in the file
    • BTo detect the end of the file
    • CTo count the number of lines
    • DTo remove empty lines from the output
  8. 8.Which Python statement correctly opens a file named 'students.txt' for appending?

    Easy
    • Afile = open('students.txt', 'a')
    • Bfile = open('students.txt', 'w')
    • Cfile = open('students.txt', 'r')
    • Dfile = open('students.txt', 'append')

Unlock all 57 questions & more

無料アカウントを作って、このトピックのすべての問題・スライド・フラッシュカード・復習ノートを見よう。

過去問

このトピックの過去問練習は近日公開。
近日公開