310 likes | 422 Vues
Text Files. CSCE 110. From James Tam’s material. What You Know About Input And Output. Comes from the user or is displayed to the user. Person to program (read / readln). Program to person (write / writeln). Input And Output Using Files.
E N D
Text Files CSCE 110 From James Tam’s material
What You Know About Input And Output Comes from the user or is displayed to the user Person to program (read / readln) Program to person (write / writeln)
Input And Output Using Files Information is retrieved from and written out to a file (typically on disk). File to program (read / readln) Program to file (write / writeln) File (on disk)
Why Bother With Files? • Too much information to input all at once • The information must be persistent (RAM is volatile) • Etc.
What You Need In Order To Read/Write Information From/To A File • Declare a file variable • Open the file • A command to read/write the information
Declaring File Variables Allows the program access to a text file No difference in the declaration of a file variable when writing to a file from the case of reading from a file. Format: name of file variable : text; Example: letterGrades : text; gradePoints : text; A new type for us!
Opening Files for Reading Prepares the file for reading: • Links the file variable with the physical file (references to the file variable are references to the physical file). • Positions the file pointer. Format: reset (name of file variable, location and name of file);1 Example: (File variable declaration for constant or variable filename) var letterGrades : text; (Constant file name) reset (letterGrades, ‘letterGrades.txt’); OR (Variable file name) var inputFile : string [80]; readln(inputFile); reset(letterGrades, inputFile); 1 If no location is provided then the program will look in the current directory for the file.
Linking The File Variable With The Physical File Your program : File variable (letterGrades) letterGrades.txt (text file) : : :
Positioning The File Pointer letterGrades.txt A B C B B :
Reading Information From Files Performed with read or readln Format: read (name of file variable, variable to store the information); readln (name of file variable, variable to store the information); Example: readln(letterGrades, letter);
Reading Information From Files (2) Typically reading is done within the body of a loop Format: while NOT EOF (name of file variable) do begin read (name of file variable, variable to store the information); OR readln (name of file variable, variable to store the information); end; (* Done reading from input file *) EOF prevents us from trying to read the end of file marker (which would crash the program).
Example while NOT EOF (letterGrades) do begin readln(letterGrades, letter); writeln(letter); end; (* Loop to read letter grades file *)
Reading From Files: Putting It All Together program grades (output); const FILENAME_LENGTH = 256; type stringType = string[FILENAME_LENGTH]; begin var letterGrades : text; var letter : char; var inputFile : stringType; write('Enter name of input file: '); readln(inputFile); reset(letterGrades, inputFile); writeln('Opening file ', inputFile, ' for reading.');
Reading From Files: Putting It All Together (2) while NOT EOF (letterGrades) do begin readln(letterGrades, letter); writeln(letter); end; close(letterGrades); writeln('Completed reading of file ‘, inputFile); end. This works when we know exactly what’s on each line.
Unknown Length of Line Format: while NOT EOF (name of file variable) do begin while NOT EOLN (name of file variable) do begin {process line} read (name of file variable, variable to store the information); ... end; readln (name of file variable) end { Done reading line } end; { Done reading from input file } EOLN lets us know when we reach the end of the line so we can call readln to move the file pointer to the next line.
Unknown Length of Line: Example while NOT EOF (letterGrades) do begin while NOT EOLN (letterGrades) do begin {process line} read (letterGrades, letter); write (letter); end; readln (letterGrades); writeln; end { Done reading line } end; { Done reading from input file }
Opening Files for Writing Two methods: • Rewriting – erases the old contents of the file and replaces it (rewrites over what was already there). • Appending – retain the old contents of the file and adds to it (appends the new information at the end). Format (rewriting / appending): rewrite (name of file variable, location and name of physical file); append (name of file variable, location and name of physical file); Example (rewriting / appending): (Constant file name) var gradePoints : text; rewrite(gradePoints, ‘gradePoints.txt’); append(gradePoints, ‘gradePoints.txt’);
Opening The File (2) Example (rewriting / appending): (Variable file name) const SIZE = 256; : var outputFile : string[SIZE]; var gradePoints : text; write(‘Enter the name of the output file: ‘); readln (outputFile); rewrite(gradePoints, outputFile); OR append(gradePoints, outputFile);
Writing To A File Format: write (name of file variables, variable(s) and/or strings to write); writeln (name of file variables, variable(s) and/or strings to write); Example: writeln(gradePoints, gpa);
Passing File Variables As Parameters Must always be passed by reference (i.e. as var parameters). Format: procedure nameProcedure (var nameFile : text); Example: procedure fileInputOuput (var letterGrades : text; var gradePoints : text);
Writing To A File: Putting It All Together program grades (output); const FILE_NAME_LENGTH = 256; begin var letterGrades : text; var gradePoints : text; var letter : char; var gpa : integer; var inputFileName : string[FILE_NAME_LENGTH]; var outputFileName : string[FILE_NAME_LENGTH];
Writing To A File: Putting It All Together (2) write('Enter the name of file that contains the letter grades: '); readln(inputFileName); write('Enter the name of the file to store the grade points: '); readln(outputFileName); reset(letterGrades, inputFileName); rewrite(gradePoints, outputFileName); writeln('Opening file ', inputFileName, ' for reading.'); writeln('Opening file ', outputFileName, ' for writing.');
Writing To A File: Putting It All Together (3) while NOT EOF (letterGrades) do begin readln(letterGrades, letter); case (letter) of 'A' : gpa := 4; 'B' : gpa := 3; 'C' : gpa := 2; 'D' : gpa := 1; 'F' : gpa := 0; else gpa := -1; end; (* case *) writeln(gradePoints, gpa); end; (* Loop to read letter grades file *)
Writing To A File: Putting It All Together (4) writeln('Completed reading and writing to files.'); close(letterGrades); close(gradePoints); end.
The Consequence Of Reading Past The End Of File Input file: file.dat A Pascal program: EOFExample.p program EOFExample; begin var inputFile : text; var ch : char; reset(inputFile, ’file.dat’); readln(inputFile, ch); readln(inputFile, ch); close(inputFile); end;
The Consequence Of Reading Past The End Of File Input file: file.dat A Pascal program: EOFExample.p program EOFExample; begin var inputFile : text; var ch : char; reset(inputFile, ’file.dat’); readln(inputFile, ch); readln(inputFile, ch); close(inputFile); end; First readln: Character read from file
The Consequence Of Reading Past The End Of File Input file: file.dat A Pascal program: EOFExample.p program EOFExample; begin var inputFile : text; var ch : char; reset(inputFile, ’file.dat’); readln(inputFile, ch); readln(inputFile, ch); close(inputFile); end; Second readln: runtime error (read past the end of the file)
Explicitly Dealing With Empty Files Recall: EOF (name of file variable) • Q: Has the end of file been reached? • Returns true if the file pointer is not pointing at a character in the file. • Returns false if the file pointer is pointing at a character in the file.
Explicitly Dealing With Empty Files: An Example program fileExampleThree (input,output); const FILENAME_LENGTH = 256; LINE_LENGTH = 80; begin var inputFilename : string[FILENAME_LENGTH]; var inputFileVariable : text; var line : string[LINE_LENGTH]; write('Enter the name of the input file: '); readln(inputFilename); reset(inputFileVariable,inputFilename);
Explicitly Dealing With Empty Files: An Example (2) if EOF (inputFileVariable) then begin writeln('File ', inputFilename, ' is empty.'); end else begin writeln('Opening file ', inputFilename, ' for reading'); while NOT EOF (inputFileVariable) do begin readln(inputFileVariable,line); writeln(line); end; end; writeln(‘Closing file ‘, inputFilename); close(inputFileVariable); end.