logo

Cambridge IGCSE Computer Science revision notes

File handling Revision Notes

Cambridge IGCSE revision notes for File handling.

At a glance

revision notes

Resource type

Topic

File handling

CambridgeIGCSEComputer ScienceProgramming

Complete revision guide 1 of 2

Why Data is Stored in Files for Program Use

Data is stored in files to provide persistent, organised, and efficient access for programs, enabling data to be saved beyond program execution and managed effectively.

Syllabus source

Section 3, Topic 8.3, page 29, objective 8.3.1

Open source document ↗

What you should be able to do

  • ✓Explain the need for data persistence in programming.
  • ✓Describe how files help organise data for easier access and manipulation.
  • ✓Understand how file storage supports efficient memory use during program execution.
1

Data Persistence Beyond Program Execution

When a program runs, it often needs to use data that should remain available even after the program stops. Storing data in files allows this persistence because files are saved on non-volatile storage devices like hard drives or SSDs. Unlike variables stored in RAM, which lose their values when the computer is turned off or the program ends, files keep data intact until explicitly changed or deleted.

For example, a student record system saves student details in a file so that the information is still available the next day when the program is run again. Without files, all data would be lost once the program closes, requiring re-entry every time.

  • Files provide long-term storage of data beyond program runtime.
  • Non-volatile storage ensures data is not lost when power is off.
  • Variables in memory are temporary and cleared after program ends.
2

Organising Data for Easy Access and Manipulation

Files allow data to be stored in a structured way, such as lines of text or records, which makes it easier for programs to find and use specific pieces of information. Organised data can be read sequentially or accessed randomly depending on the file type and program design.

For instance, a program that manages a library catalogue might store each book’s details on a separate line in a text file. This organisation helps the program quickly locate a particular book’s information without scanning unrelated data.

This structure also supports data manipulation, such as updating, deleting, or adding records, by reading and rewriting parts of the file as needed.

  • Structured files enable targeted data retrieval.
  • Organisation reduces complexity when searching or modifying data.
  • Supports operations like reading, updating, and appending data efficiently.
3

Efficient Data Management and Memory Usage

Loading all data into a program’s memory at once can be inefficient or impossible if the data is large. Files allow programs to read only the necessary data when needed, reducing memory consumption and improving performance.

For example, a video game might store player statistics in a file and load only the current player’s data into memory during gameplay, rather than loading all players’ data at once.

This selective access is crucial for programs running on devices with limited memory or when handling large datasets.

  • Files enable partial data loading to save memory.
  • Improves program speed by avoiding unnecessary data processing.
  • Essential for handling large or multiple datasets efficiently.
4

Summary and Practical Implications

Storing data in files is fundamental for programs that require data to be saved permanently, organised logically, and accessed efficiently. Understanding this helps in designing programs that manage data effectively, avoid data loss, and perform well.

When writing programs, consider what data needs to be persistent, how it should be structured in files, and how to access it without overloading memory. This approach leads to robust and user-friendly software.

  • Files ensure data is saved and available for future use.
  • Proper file organisation simplifies program design and maintenance.
  • Efficient file handling optimises resource use and program responsiveness.

At-a-glance summary

Comparison table

StepDetailExampleExam Use
Data PersistenceSaving data beyond program executionStudent records saved in a file to retain information after closing the programExplain why variables alone cannot store data permanently
Data OrganisationStructuring data for easy accessEach book’s details on a separate line in a text fileDescribe how files help locate and manipulate specific data
Efficient AccessLoading only needed data into memoryGame loads only current player stats from fileDiscuss memory benefits of partial data loading from files
Data ManipulationUpdating or adding data in filesAppending new customer orders to a sales fileExplain how files support data updates without losing existing data

Active recall

Quick check

1. Why can’t data stored in variables alone be used to save information permanently?

Because variables are stored in RAM, which is volatile and loses data when the program ends or the computer is turned off.

2. How does storing data in files help a program manage large datasets?

Files allow the program to load only the necessary parts of the data into memory, reducing memory usage and improving efficiency.

3. Give an example of how data organisation in files aids program functionality.

Storing each customer’s details on separate lines in a file makes it easier for the program to find and update a specific customer’s information.

Complete revision guide 2 of 2

File Operations: Opening, Closing, and Manipulating Files for Reading and Writing

This note explains how to perform essential file operations in programming, including opening and closing files, and manipulating their contents by reading and writing individual data items and lines of text. Understanding these operations is fundamental for managing persistent data within programs.

Syllabus source

Section 3, Topic 8.3, page 29, objective 8.3.2

Open source document ↗

What you should be able to do

  • ✓Understand how to open files in different modes for reading and writing.
  • ✓Learn why closing files is necessary and how to do it properly.
  • ✓Be able to read individual data items and entire lines from a file.
  • ✓Be able to write individual data items and lines to a file.
  • ✓Recognise the importance of file manipulation for data persistence and program functionality.
1

Opening Files for Reading and Writing

Before a program can access data stored in a file, it must open the file. Opening a file establishes a connection between the program and the file stored on disk or other storage media.

Files can be opened in different modes depending on the intended operation:

Read mode ('r'): Opens an existing file to read its contents. The file must exist; otherwise, an error occurs. No writing is allowed in this mode, and the file pointer starts at the beginning of the file. Example: opening 'data.txt' to read stored scores line by line for processing in a program.

  • Opening a file in read mode allows data retrieval but prevents modification.
  • Opening a file in write mode ('w') creates a new file or overwrites an existing one, allowing data to be written.
  • Append mode ('a') opens a file to add data at the end without deleting existing content.
2

Closing Files and Its Importance

After completing operations on a file, it is essential to close it. Closing a file ensures that all data buffered in memory is properly saved to the storage device and that system resources are freed for other processes.

Failing to close a file can lead to data loss, corruption, or resource leaks, which may cause a program or system to behave unpredictably.

Example: After writing new user information to 'users.txt', closing the file guarantees that the data is fully saved and the file is no longer locked by the program.

  • Always close files after finishing reading or writing.
  • Use commands or functions specific to the programming language to close files.
  • Some languages automatically close files when the program ends, but explicit closing is best practice.
3

Reading Data from Files: Items and Lines

Reading from a file can be done at different granularities: individual data items or entire lines of text.

Reading individual data items involves extracting specific pieces of data, such as numbers or words, often separated by delimiters like spaces or commas.

Reading lines involves retrieving a whole line of text at once, which is useful for processing structured data line by line, such as records in a CSV file or paragraphs in a text document.

  • Use functions or methods to read one line at a time, which returns the line as a string including or excluding the newline character.
  • To read individual data items, parse the line by splitting it based on delimiters.
  • Example: Reading a line '23,45,67' and splitting it by commas to get three separate numbers.
4

Writing Data to Files: Items and Lines

Writing to a file can also be done by outputting individual data items or entire lines of text.

When writing individual items, the program may convert data types to strings and add delimiters to separate items.

Writing lines involves outputting a complete string followed by a newline character to ensure data is stored in a readable format line by line.

  • Writing overwrites existing content if the file is opened in write mode, or appends if opened in append mode.
  • Example: Writing a list of scores to a file, each on a new line, to preserve readability and structure.
5

Practical Example: Reading and Writing a File

Consider a program that reads a file 'scores.txt' containing student scores, calculates the average, and writes the result to 'average.txt'.

Steps: 1) Open 'scores.txt' in read mode. 2) Read each line, convert the text to numbers, and accumulate the total and count. 3) Close 'scores.txt'. 4) Calculate the average by dividing total by count. 5) Open 'average.txt' in write mode. 6) Write the average as a line of text. 7) Close 'average.txt'.

This example demonstrates opening files for reading and writing, reading individual data items (scores), writing a line of text (average), and closing files properly.

  • Always check if files open successfully before proceeding.
  • Handle possible errors such as missing files or permission issues.

At-a-glance summary

Comparison table

StepDetailExampleExam Use
Opening a fileEstablish connection to file in specific modeOpen 'data.txt' in read mode to access contentsState mode used and file name when asked
Reading dataExtract data items or lines from fileRead each line from 'scores.txt' and split into numbersExplain how data is read and processed
Writing dataOutput data items or lines to fileWrite average score as a line to 'average.txt'Describe writing method and data format
Closing a fileTerminate connection and save dataClose 'scores.txt' after reading to free resourcesAlways mention closing files to gain marks

Active recall

Quick check

1. What mode should you use to open a file if you want to read its contents without changing it?

Read mode ('r') should be used to open a file for reading without modifying it.

2. Why is it important to close a file after writing data?

Closing a file ensures all data is saved from memory to storage and frees system resources.

3. How can you read individual data items from a line of text in a file?

Read the line as a string, then split it using delimiters like commas or spaces to extract individual items.

4. What happens if you open a file in write mode that already exists?

The existing file is overwritten, and its previous contents are lost.

Related topics

Study nearby topics next

Revision hubs

Keep studying from the right hub