Input and Output
When a program needs to save data for later use, it writes the data to a file. The data can be read from the file at a later time. When a program runs, the data is stored in RAM (Random Access Memory). Once the program stops, the data is lost - UNLESS it has been saved to a file. Most software stores data in files. For example:
- Word processors - used to write letters, reports, etc. that are saved to a file.
- Image editors - used to draw graphics, edit photos, etc. that are saved to a file.
- Spreadsheets - used to create budgets, etc. that are saved to a file.
- Games - keep data like top scores, list of players, etc. that are saved to a file.
- Web browsers - store login information in cookies, etc. that are saved to a file.
Business software relies extensively on data stored in files; payroll software, inventory software, etc. Programmers refer to the process of saving data to a file as writing data to a file. When data is written to a file, it is copied from a variable in RAM to the file. The term output file is used to describe a file that data is written to - because the program stores output in the file.
The process of retrieving data from a file is known as reading data from a file. When data is read from a file, it is copied from the file to a variable in RAM. The term input file is used to describe a file that data is read from - because the program gets input from the file.
We're about to discuss how to write programs that write data to files and read data from files. There are three steps that must be taken when a files is used by a program.
- Open the file - creates a connection between the file and the program. Opening an output file creates the file on disk and allows the program to write data to it. Opening a file for input allows the program to read data from the file.
- Process the file - in this step data is either written to the file (if an output file) or read from the file (if an input file).
- Close the file - once finished with the file, it must be closed. Closing the file disconnects the file from the program.
Types of Files
There are two types of files: (1) text, and (2) binary. A text file contains data that has been encoded as text, using a scheme such as ASCII or Unicode. Even if a file contains numbers, those numbers are stored in the file as a series of characters. As a result, the file may be opened in a text editor such as Notepad. A binary file contains data that has not been converted to text. Consequently, we can not view the contents of a binary file with a text editor.
File Access Methods
Most programming languages provide two ways to access data stored in a file
- Sequential access - starts from the beginning of the file and proceeds to the end of the file. Can not jump directly to data. Just like a cassette tape - have to listen to songs from start to end.
- Direct access - also known as a random access file, we can jump directly to any piece of data in the file without having to read the data that comes before it. Just like a CD or MP3 player - can jump to any song.
We'll focus on sequential access files to gain an understanding of basic file operations.