Test your knowledge with free interactive questions on Seneca — used by over 10 million students.

Files

Without files, we can only store data in the memory.

Files

Files

  • Files allow us to store data so that it persists even when our program is not running.
  • This allows programs to 'carry on where they left off' when they are executed.
Text files

Text files

  • Text files consist of string data that can be read and processed by many applications.
Binary files

Binary files

  • Binary files consist of binary data that is read and processed by specific applications.

File Handling

We must be careful with the way we access files so that we do not accidentally corrupt them.

Open the file

Open the file

  • First we open the file for use, so that the operating system knows that we are using it.
  • We can do this using:
    • openRead("filename.txt")
    • openWrite("filename.txt")
Assign the file to a variable

Assign the file to a variable

  • The file must be assigned to a variable so that we can keep refering to it.
  • This can be done as follows:
    • myFile = openRead("filename.txt")
    • myFile = openWrite("filename.txt")
Read / write

Read / write

  • We can now read or write to the file using the following commands:
    • myFile.readLine() will return the next line of a file as a string.
    • myFile.writeLine() will write a line to the file.
    • myFile.endOfFile() will return True if we reach the end of the file.
Close the file

Close the file

  • Finally, we must close the file so that it is free for another program to access.
  • We can do this using:
    • myFile.close()
Jump to other topics
1

Computer Systems

1.1

Systems Architecture

1.2

Memory & Storage

1.3

Computer Networks, Connections & Protocols

1.4

Network Security

1.5

Systems Software

1.6

Ethical, Legal, Cultural & Environmental Concern

2

Computational Thinking, Algorithms & Programming

2.1

Algorithms

2.2

Programming Fundamentals

2.3

Producing Robust Programs

2.4

Boolean Logic

2.5

Programming Languages & IDEs

Practice questions on File Handling

Can you answer these? Test yourself with free interactive practice on Seneca — used by over 10 million students.

  1. 1
    Types of file:Fill in the list
  2. 2
  3. 3
  4. 4
    File HandlingPut in order
  5. 5
Answer all questions on File Handling

Unlock your full potential with Seneca Premium

  • Unlimited access to 10,000+ open-ended exam questions

  • Mini-mock exams based on your study history

  • Unlock 800+ premium courses & e-books

Get started with Seneca Premium