பீட்டாஇந்த தளம் தீவிர வளர்ச்சியில் உள்ளது; பிழைகள், விடுபட்ட அம்சங்கள் மற்றும் தரவு இழப்பு அபாயம் உள்ளன. உங்கள் ஆதரவுக்கு நன்றி!

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

இந்த தலைப்பிற்கான ஒவ்வொரு கேள்வியையும், ஸ்லைடுகளையும், ஃப்ளாஷ் கார்டுகளையும் மற்றும் திருப்புதல் குறிப்புகளையும் பார்க்க இலவச கணக்கை உருவாக்கவும்.

முந்தைய தேர்வுகள்

இந்த தலைப்பிற்கான முந்தைய தேர்வு பயிற்சி விரைவில் வருகிறது.
விரைவில் வருகிறது