베타이 플랫폼은 활발히 개발 중이에요; 버그, 미완성 기능, 데이터 손실 위험이 있어요. 응원해 주셔서 감사해요!

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

무료 계정을 만들어 이 주제의 모든 문제, 슬라이드, 플래시카드, 복습 노트를 확인하세요.

기출 문제

이 주제의 기출 문제 연습이 곧 나와요.
곧 출시