測試版本平台正在積極開發中;可能有錯誤、缺少功能,以及資料遺失的風險。感謝你的支持!

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

建立免費帳號,即可查看這個主題的所有題目、投影片、字卡與複習筆記。

歷屆試題

這個主題的歷屆試題練習即將推出。
即將推出