Docsity

Lecture Notes on Files and File Processing | CSC 112, Study notes of Computer Science

Toggle sidebar
bg1
Files and File Processing
But think of it as “Songs” and “Music Processing.” Nearly
everybody is familiar with that!
Historically, songs have been stored in many formats. A
magnetized wire, grooves in vinyl, various tape formats (cassettes;
8-track), and nowadays in various digital formats.
Music is data. A album is a file. Each song is a record. So
Radiohead’s album “In Rainbows” is like a file; each individual
song such as “Nude” is like a record.
A file is a collection of records with some common theme, such as
songs by a certain group, or data from a certain experiment, or
transactions from a certain bank, and so on.
Files are useful because they store large amounts of data that can
be easily moved around and manipulated. An album is far more
convenient than a bunch of individual songs, let alone a bunch of
individual notes of a song.
1
pf3
pf4
pf5
pf8

Partial preview of the text

Download Lecture Notes on Files and File Processing | CSC 112 and more Study notes Computer Science in PDF only on Docsity!

Files and File Processing But think of it as “Songs” and “Music Processing.” Nearly everybody is familiar with that! Historically, songs have been stored in many formats. A magnetized wire, grooves in vinyl, various tape formats (cassettes; 8-track), and nowadays in various digital formats. Music is data. A album is a file. Each song is a record. So Radiohead’s album “In Rainbows” is like a file; each individual song such as “Nude” is like a record. A file is a collection of records with some common theme, such as songs by a certain group, or data from a certain experiment, or transactions from a certain bank, and so on. Files are useful because they store large amounts of data that can be easily moved around and manipulated. An album is far more convenient than a bunch of individual songs, let alone a bunch of individual notes of a song.

1. Opening Files When you go to play or “input” music, let’s say a CD, you must first open the CD case. Likewise, when you need a file of data for input into a computer program, you must open that file with a Fortran command. Then you choose a device to play it on; maybe the player in your home, or in your car, or maybe even an old-fashioned portable player. This is the input unit. Same with the computer. You may input through the keyboard, from memory, or from your hard drive, from a “key” and so on. You issue a Fortran command to identify the input unit. Let’s look at a little Fortran. Here is the open statement , or the start of one. OPEN (UNIT=12, FILE= “In Rainbows.mus”... The 12 is the code for the input device (or CD player to continue the musical metaphor). It becomes associated with the file (or songs) of interest. Your lab instructor will give you the proper unit numbers. It will correspond to some source of data. The alpha-numeric that follows the key word FILE is the computer name of the file of data you are interested in, and is like the name of the album. The next keyword muddies our musical metaphor, but if we replace the CD by cassette tape (a what?), you can imagine that you might want to: a) listen to the album, b) make your own album by singing a bunch of songs and recording over what’s on the tape, or c) make your own album by singing a bunch of songs and recording onto a fresh tape.

With computer data files you can provide an “error flag” that will be set to certain values depending on how the computer is feeling toward your file. This flag may then be examined to see what’s up. The keyword here is IOSTAT (“ input/output status”), and you choose your own variable name to hold the info. It looks like IOSTAT = a_variable_you_give If the open statement is successful, a zero will be returned in the integer variable (a_variable_you_give). If not successful, a positive number corresponding to a system error message will be returned in the variable. Continuing our example: INTEGER:: ioflag OPEN (UNIT=12, FILE= “In Rainbows.mus”, STATUS= “OLD”, ACTION= “READ”, IOSTAT=ioflag) And that’s all there is to it. Fairly straightforward, wouldn’t you say? After the OPEN you can write some code like: IF (ioflag /= 0) THEN WRITE (,) “Error reading file” STOP END IF

Here is an example of opening a file that is to be created: OPEN (UNIT = 13, FILE = "newbie.dat", STATUS="NEW", ACTION = "WRITE", IOSTAT = ierror) The STATUS="NEW" specifies that the file must be created. However, if the file already exists, there will be an error message number in the ierror variable stating that the file already exists. If the programmer doesn't mind replacing a file with a new file then the programmer can use the STATUS = "REPLACE" statement. There is one last statement that has not been explained, the STATUS = "SCRATCH" statement. With this statement, no file name can be specified. This is simply a temporary (scratch) file that can hold information during the execution of the program. Once the program has terminated the scratch file no longer exists. The statement is simplicity itself: OPEN (UNIT = 13, STATUS="SCRATCH”)

OPEN (UNIT = 12, FILE = "input.dat", STATUS = "OLD", IOSTAT = ierror) READ(12,) x, y, z! 12 is the unit number of the file* This READ statement will read x, y, and z from the first line of the input file. Here’s another example: OPEN (UNIT = 13, FILE = "output.dat", STATUS ="REPLACE", IOSTAT = ierror) 100 FORMAT (' X = ', F10.2,' Y = ', F10.2, ' Z = ', F10.2) WRITE (13,100) x, y, z This will write the values of x, y, and z to the file output.dat in the format specified by the 100 FORMAT statement.

4. There are two additional commands that are very useful when processing files. The music analogy is apropos here. Often you want to hear your music starting with the first song. You can set the track to 1 on a CD or DVD, or rewind a tape to the beginning, or just touch a button on your digital play which might well be called “rewind.” REWIND : This command is used to “rewind” or re-start the file at its beginning. This is useful when you need to reuse the values in the file. Here it is: REWIND(12) Whatever file is associated with unit number 12 is such that when you READ or WRITE, you’ll be at the start of the file.

Sometimes you want to here a song or two over again without going back to the beginning of the album. All music players allow you to go back one song, or more formally, to “backspace.” You can do the same thing in a data file. BACKSPACE : This command is used to back up a single record in a file. If you wanted to back up three records, you would use the command three times. The syntax for this command is as follows: BACKSPACE(17) Back up one song, oops, record, in the file associated with unit 17.