Starting Out with Programming Logic and Design (2nd Edition) |
1. Introduction to File Input and Output
All programs looked at so far make us enter information each time. Programs can be allowed to read data from a disk as well as write it to a disk to be used later. An output file is one that a program writes to. An input file is one that a program would read data from. To use any data a program must:
- Open the file - make it so that the file is available to read or write.
- Process the file - information is either read from or written to the disk.
- Close the file - disconnect itself from the file so it cannot use it any more.
Types of Files
Text - alphabetic and numeric characters as well as some symbols. Can be open in a simple text editor.
Binary - data that has been stored in a special way and cannot be read easily in a text editor.
File Access Methods
In accessing data from a disk the files are stored two ways.
Sequential - think cassette tape. In order to get to a certain song you must go through each one to get to it. Likewise with data, if you the last piece of data you have to go through all the rest of them.
Direct access - (random access) think CD player, you can access the last song directly. Likewise with direct access, you can jump directly to the data you want.
We will be looking at sequential access files in this chapter.
Creating a File and Writing Data to it
The general way to create a file is multi-stepped:
- Use a declare statement - general format - declare outputfile filename - filename is a name we use in the program, outputfile is the name of the file we will save it as
- Next we open the file - general format - open filename "customer.dat" - filename like before, customer.dat the name of the file we will create. WARNING - If the file exist it will be overwritten
- Write the data to the file (actually file buffer) - general format - write filename data - data is the data that you want to write
- Close the file - whatever information that has not been written from the buffer gets done now and file is marked as closed.
Delimiters and the EOF Marker
A delimiter is a piece of information that separates the data into recognizable chunks. An EOF (end of file) marker is a piece of information that tells the program the data has stopped and the file goes no further.
Reading Data From a File
Reading data from a file is similar Process.
Declare Inputfile filename
Open filename "diskfile.name"
Read filename itemname
Similar naming as above only now the read takes information in to memory. We then close the file.
Close filename
When the program reads in data it will read to the delimiter and then make a pointer to where it left off. The next time it reads it will pick up from that pointer (read position).
Appending Data to an Existing File
As noted before, opening a file to be written to will cause the file to be overwritten if it exists. For this reason programming languages use an append mode.
Declare output file appendmode filename
Open filename "nameOn.dsk"
Write filename data
2. Using Loops to Process Data
Usually if a file is used to read or write data on the drive a loop is involved.
Reading a File with a Loop and Detecting the End of the File
Often we will not know how much data we have so we cannot use a numbered loop. Most languages provide for this with an EOF function that the while loop can test for. As soon as the EOF marker is noted, it will go true and drop out of the loop.
3. Using Files and Arrays
By using loops, we can loop through an array and move the data to a file by writing one element at a time to the file. Likewise we could read the data in one chunk at a time and then place it into the array.
4. Processing Records.
When data is written it is often done in records and fields. A field is a piece of data, and all the fields that are related to one item are records.
Writing Records
By using one write statement we can put several variables of data together and write a record at a time.
Reading Records
Likewise we can read in several variables of information at one time by doing a read statement with multiple variable names.
5. Control Break Logic
Often there is q need to limit the amount of information that will display. For this we will do a control break routine. To do this put a counter in the loop that displays information and when it gets to the maximum display, display a message' wait for a keystroke and the reset counter to start again.
Hello World! Computer Programming for Kids (and Other Beginners): Computer Programming for Kids and Other Beginners |
No comments:
Post a Comment