Programs process and use ____. When the program finishes, or is closed, any data it held is lost. To prevent loss, data can be stored in a ____ so that it can be accessed again at a later date. Files have two modes of operation: 1. ____, where the file is opened so that data can be read from it. 2. Write, where the file is opened so that data can be ____ to it. Each item of data written to or from a file is called a record. Files have ____ modes of operation - read from and write to. Opening and closing files A file must be ____ before a record can be read from or written to it. To open a file, it must be referred to by its ____ (name), for example: file = openRead("scores.txt") This would open the ____ scores.txt and allow its contents to be ____ file = openWrite("scores.txt") This would open the file scores.txt and allow it to have data ____ to it. file.close() This would ____ the file. Reading from a file Once a file has been opened, the records are read from it one line at a time. The data held in this record can be ____ into a variable, or, more commonly, an ____ (list), for example: file = openRead("scores.txt") score = myFile.readLine() file.____ An array example is shown below: ____ = openWrite("scores.txt") for x = 0 to 9 scores____ = myFile.readLine() ____ file.close() End of file When reading a file that has more than one record, the computer needs to know what to do when there are no more records to ____. The endOfFile() statement checks to see if the ____ record has already been read, for example: ____ = openRead("scores.txt") while NOT file.endOfFile() scores____ = myFile.readLine() x = x + 1 endwhile file.____ The above code would read each record and place it into an ____ called scores. The operations ____ when there are no more files to read. Arrays are often used to hold data read from files.

2.2 Basic File Handling Operations

Top-lista

Vizualni stil

Postavke

Promijeni predložak

Vrati automatski spremljeno: ?