登录

File handling

Handling text files

Text files consist of lines of text that are read or written consecutively as strings.

A file must be opened in a specified mode before any file operations are attempted. This is written as follows:

OPENFILE <file identifier> FOR <file mode>

The file identifier may be a literal string containing the file names, or a variable of type STRING that has been

assigned the file name.

The following file modes are used:

  • READ for data to be read from the file

  • WRITE for data to be written to the file. A new file will be created and any existing data in the file will be lost.

  • APPEND for data to be added to the file, after any existing data.

A file should be opened in only one mode at a time.

Data is read from the file (after the file has been opened in READ mode) using the READFILE command a follows:

READFILE <file identifier>, <variable>

The variable should be of data type STRING. When the command is executed, the next line of text in the file is read and assigned to the variable.

The function EOF is used to test whether there are any more lines to be read from a given file. It is called as follows:

EOF(<file identifier>)

This function returns TRUE if there are no more lines to read (or if an empty file has been opened in READ

mode) and FALSE otherwise.

Data is written into the file (after the file has been opened in WRITE or APPEND mode) using the

WRITEFILE command as follows:

WRITEFILE <file identifier> , <data>

Files should be closed when they are no longer needed using the CLOSEFILE command as follows:

CLOSEFILE <file identifier>
//Example – handling text files
//This example uses the operations together, to copy all the lines from FileA.txt to FileB.txt, replacing any blank lines by a line of dashes.
DECLARE LineOfText : STRING
OPENFILE "FileA.txt" FOR READ
OPENFILE "FileB.txt" FOR WRITE
WHILE NOT EOF("FileA.txt")
    READFILE "FileA.txt", LineOfText
    IF LineOfText = "" THEN
        WRITEFILE "FileB.txt", "----------------------------"
    ELSE
        WRITEFILE "FileB.txt", LineOfText
    ENDIF
ENDWHILE
CLOSEFILE "FileA.txt"
CLOSEFILE "FileB.txt"

readline

file.readline([size])

f1 = open("a.txt", 'r')
content = f1.readline()
print(content) #Output first line of a.txt
f1.close()

write

file.write(str)

f1 = open("a.txt", 'w')
f1.write('hello') #write hello to a.txt
f1.close()

Handling random files

Random files contain a collection of data, normally as records of fixed length. They can be thought of as having a file pointer which can be moved to any location or address in the file. The record at that location can then be read or written.

Random files are opened using the RANDOM file mode as follows:

OPENFILE <file identifier> FOR RANDOM

As with text files, the file identifier will normally be the name of the file. The SEEK command moves the file pointer to a given location:

SEEK <file identifier>, <address>

The address should be an expression that evaluates to an integer which indicates the location of a record to be read or written. This is usually the number of records from the beginning of the file. It is good practice to explain how the addresses are computed.

The command GETRECORD should be used to read the record at the file pointer:

GETRECORD <file identifier>, <variable>

When this command is executed, the record that is read is assigned to the variable which must be of the appropriate data type for that record (usually a user-defined type).

The command PUTRECORD is used to write a record into the file at the file pointer:

PUTRECORD <file identifier>, <variable>

When this command is executed, the data in the variable is inserted into the record at the file pointer. Any data that was previously at this location will be replaced.

//Example – handling random files
//The records from positions 10 to 20 of a file StudentFile.Dat are moved to the next position and a new record is inserted into position 10. The example uses the user-defined type Student defined in Section 4.1.
DECLARE Pupil : Student
DECLARE NewPupil : Student
DECLARE Position : INTEGER

NewPupil.LastName ←"Johnson"
NewPupil.Firstname ←"Leroy"
NewPupil.DateOfBirth ←02/01/2005
NewPupil.YearGroup ← 6
NewPupil.FormGroup ← ꞌAꞌ

OPENFILE "StudentFile.Dat" FOR RANDOM
FOR Position ← 20 TO 10 STEP -1
    SEEK "StudentFile.Dat", Position
    GETRECORD "StudentFile.Dat", Pupil
    SEEK "StudentFile.Dat", Position + 1
    PUTRECORD "StudentFile.Dat", Pupil
NEXT Position

SEEK "StudentFile.Dat", 10
PUTRECORD "StudentFile.Dat", NewPupil

CLOSEFILE "StudentFile.dat"

登录