DOC PREVIEW
TAMU CSCE 110 - text Files
Type Lecture Note
Pages 31

This preview shows page 1-2-14-15-30-31 out of 31 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 31 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 31 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 31 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 31 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 31 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 31 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 31 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Text FilesWhat You Know About Input And OutputInput And Output Using FilesWhy Bother With Files?What You Need In Order To Read/Write Information From/To A FileDeclaring File VariablesOpening Files for ReadingLinking The File Variable With The Physical FilePositioning The File PointerReading Information From FilesReading Information From Files (2)ExampleReading From Files: Putting It All TogetherReading From Files: Putting It All Together (2)Unknown Length of LineUnknown Length of Line: ExampleOpening Files for WritingOpening The File (2)Writing To A FilePassing File Variables As ParametersExample: Copying a FileWriting To A File: Putting It All TogetherWriting To A File: Putting It All Together (2)Writing To A File: Putting It All Together (3)Writing To A File: Putting It All Together (4)The Consequence Of Reading Past The End Of FileSlide 27Slide 28Explicitly Dealing With Empty FilesExplicitly Dealing With Empty Files: An ExampleExplicitly Dealing With Empty Files: An Example (2)J. Michael MooreText FilesCSCE 110From James Tam’s materialJ. Michael MooreWhat You Know About Input And OutputComes from the user or is displayed to the userPerson to program (read / readln)Program to person (write / writeln)J. Michael MooreInput And Output Using FilesInformation is retrieved from and written out to a file (typically on disk).File to program (read / readln)File (on disk)Program to file (write / writeln)J. Michael MooreWhy Bother With Files?•Too much information to input all at once•The information must be persistent (RAM is volatile)•Etc.J. Michael MooreWhat You Need In Order To Read/Write Information From/To A File1. Declare a file variable2. Open the file3. A command to read/write the informationJ. Michael Moore1. Declaring File VariablesAllows the program access to a text fileNo 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!J. Michael Moore2. Opening Files for ReadingPrepares the file for reading:A. Links the file variable with the physical file (references to the file variable are references to the physical file).B. Positions the file pointer.Format: reset (name of file variable, location and name of file);1Example: (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.J. Michael MooreA. Linking The File Variable With The Physical FileYour program:File variable(letterGrades):::letterGrades.txt(text file)J. Michael MooreB. Positioning The File PointerABCBB:letterGrades.txtJ. Michael Moore3. Reading Information From FilesPerformed with read or readlnFormat: read (name of file variable, variable to store the information); readln (name of file variable, variable to store the information);Example: readln(letterGrades, letter);J. Michael Moore3. Reading Information From Files (2)Typically reading is done within the body of a loopFormat: 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).J. Michael MooreExamplewhile NOT EOF (letterGrades) do begin readln(letterGrades, letter); writeln(letter); end; (* Loop to read letter grades file *)J. Michael MooreReading From Files: Putting It All Togetherprogram 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.');J. Michael MooreReading 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.J. Michael MooreUnknown Length of LineFormat: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.J. Michael MooreUnknown Length of Line: Examplewhile 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 }J. Michael Moore2. Opening Files for WritingTwo methods:1) Rewriting – erases the old contents of the file and replaces it (rewrites over what was already there).2) 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’);J. Michael MooreOpening The File (2)Example (rewriting / appending): (Variable file name)constSIZE = 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);J. Michael Moore3. Writing To A FileFormat: 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:


View Full Document

TAMU CSCE 110 - text Files

Type: Lecture Note
Pages: 31
Documents in this Course
06-IO

06-IO

29 pages

21-OOP

21-OOP

8 pages

key

key

6 pages

21-OOP

21-OOP

8 pages

Load more
Download text Files
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view text Files and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view text Files 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?