J. Michael Moore Text Files CSCE 110 From James Tam’s material.

Slides:



Advertisements
Similar presentations
James Tam Linked Lists in Pascal Linked Lists In this section of notes you will learn how to create and manage a dynamic list.
Advertisements

I/O means Input and Output. One way: use standard input and standard output. To read in data, use scanf() (or a few other functions) To write out data,
Program CheckPass; var TestScore, ExamScore : integer; FinalScore : real; Status : string; begin write(‘Enter the Test score:’); readln(Testscore); write(‘Enter.
Slide 1 Introduction To Files In Python In this section of notes you will learn how to read from and write to text files.
James Tam Records You will learn in this section of notes how to create a new, composite type, that can be composed of different types of elements.
James Tam Introduction To Files In Python In this section of notes you will learn how to read from and write to files in your programs.
James Tam Arrays In this section of notes you will be introduced to a homogeneous composite type, one- dimensional arrays.
James Tam Sorting (Bubble) In this section of notes you will learn one technique for ordering a list.
James Tam Introduction To Files In Pascal In this section of notes you will learn how to read from and write to files in Pascal.
James Tam Sorting (Bubble) In this section of notes you will learn one technique for ordering a list.
James Tam Introduction To Files In Pascal In this section of notes you will learn how to read from and write to files in your Pascal programs.
J. Michael Moore From James Tam’s material Multi-Dimensional Arrays CSCE 110.
J. Michael Moore Other Control Structures CSCE 110 Influenced by material developed by James Tam & Jennifer Welch.
1 11/3/08CS150 Introduction to Computer Science 1 Reading from and Writing to Files Section 3.12 & 13.1 & 13.5.
J. Michael Moore Arrays CSCE 110 From James Tam’s material.
Reading and Writing Files Keeping Data. Why do we use files? ä For permanently storing data. ä For dealing with information too large to fit in memory.
J. Michael Moore Modules: Getting information out CPSC 110 Influenced by material developed by James Tam & Jennifer Welch.
James Tam Records You will learn in this section of notes how to create a new, composite type, that can be composed of different types of elements.
James Tam Arrays In this section of notes you will be introduced to a homogeneous composite type, one- dimensional arrays.
J. Michael Moore Input and Output (IO) CSCE 110 Drawn from James Tam's material.
J. Michael Moore From James Tam’s material Multi-Dimensional Arrays CSCE 110.
James Tam Arrays In this section of notes you will be introduced to a homogeneous composite type, one- dimensional arrays.
1 10/29/07CS150 Introduction to Computer Science 1 Reading from and Writing to Files Section 3.12 & 13.1 & 13.5.
J. Michael Moore From James Tam's material Records CSCE 110.
J. Michael Moore Other Control Structures CSCE 110 Influenced by material developed by James Tam & Jennifer Welch.
James Tam Records You will learn in this section of notes how to create a new, composite type, that can be composed of different types of elements.
J. Michael Moore Modules – the Basics CSCE 110 Influenced by material developed by James Tam & Jennifer Welch.
James Tam Introduction To Files In Pascal You will learn how to read from and write to text files in Pascal.
J. Michael Moore Arrays CSCE 110. J. Michael Moore Typical (although simplified) Problem Write a program that will track student grades in a class. The.
Input and Output (IO) CSCE 110 Drawn from James Tam's material.
James Tam Getting Started With Pascal Programming How are computer programs created What is the basic structure of a Pascal Program Variables and constants.
James Tam Introduction To Files In Pascal In this section of notes you will learn how to read from and write to files in Pascal.
James Tam Introduction To Files In Pascal In this section of notes you will learn how to read from and write to text files in Pascal.
J. Michael Moore Modules – the Basics CSCE 110 Influenced by material developed by James Tam & Jennifer Welch.
James Tam Introduction To Files In Python In this section of notes you will learn how to read from and write to files in your programs.
James Tam Introduction To Files In Python In this section of notes you will learn how to read from and write to files in your programs.
CMP 131 Introduction to Computer Programming Violetta Cavalli-Sforza Week 11, Lecture 2 (Wed)
STREAMS AND FILES OVERVIEW.  Many programs are "data processing" applications  Read the input data  Perform sequence of operations on this data  Write.
File I/O ifstreams and ofstreams Sections 11.1 &
Files Input and Output Program Payroll_Calculator; Uses Crt, Printer; Const A_Payrate = 100.0; B_Payrate = 50.0; Var SS_personnel : string[9]; First_Name,
Introduction to Pascal The Basics of Program writing.
Pascal Programming Files and Text (an intro). Pascal Programming Files... Data files need to be retained in secondary memory. The machine memory is random.
File I/O 1 ifstreams and ofstreams Sections 11.1 & 11.2.
Programming, an introduction to Pascal
1 File Handling. 2 Storage seen so far All variables stored in memory Problem: the contents of memory are wiped out when the computer is powered off Example:
Pascal Programming Today Chapter 11 1 Chapter 11.
1 STRINGS String data type Basic operations on strings String functions String procedures.
CS241 PASCAL I - Control Structures1 PASCAL Control Structures Modified Slides of Philip Fees.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
1 More on Readln:numerical values Note: ENTER key counts sends a carriage return and a line feed to the computer definition: “white space”: space, tab,
James Tam Introduction To Files In Python In this section of notes you will learn how to read from and write to files in your programs.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 9. Streams & Files.
CS162 External Data Files 1 Today in CS162 External Files What is an external file? How do we save data in a file?
Files A collection of related data treated as a unit. Two types Text
James Tam Introduction To Files In Python In this section of notes you will learn how to read from and write to files in your programs.
FILES AND EXCEPTIONS Topics Introduction to File Input and Output Using Loops to Process Files Processing Records Exceptions.
Python File Handling. Python language provides numerous built-in functions Some of the functions widely used for standard input and output operations.
Introduction To Files In Python
Topics Introduction to File Input and Output
Introduction To Files In Python
Brent M. Dingle Texas A&M University Chapter 12 – section 1
Computer Science II First With files.
Computer Science II First With files.
Introduction To Files In Python
Sorting (Bubble) In this section of notes you will learn one technique for ordering a list.
Files.
Topics Introduction to File Input and Output
Computer Science II Second With files.
Topics Introduction to File Input and Output
Computer Science II First With files.
Presentation transcript:

J. Michael Moore Text Files CSCE 110 From James Tam’s material

J. Michael Moore 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)

J. Michael Moore Input And Output Using Files Information 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 Moore Why Bother With Files? Too much information to input all at once The information must be persistent (RAM is volatile) Etc.

J. Michael Moore What You Need In Order To Read/Write Information From/To A File 1.Declare a file variable 2.Open the file 3.A command to read/write the information

J. Michael Moore 1.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!

J. Michael Moore 2.Opening Files for Reading Prepares 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); 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.

J. Michael Moore A.Linking The File Variable With The Physical File Your program : File variable (letterGrades) :::::: letterGrades.txt (text file)

J. Michael Moore B.Positioning The File Pointer ABCBB:ABCBB: letterGrades.txt

J. Michael Moore 3.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);

J. Michael Moore 3.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).

J. Michael Moore Example while NOT EOF (letterGrades) do begin readln(letterGrades, letter); writeln(letter); end; (* Loop to read letter grades file *)

J. Michael Moore 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.');

J. Michael Moore 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.

J. Michael Moore 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.

J. Michael Moore 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 }

J. Michael Moore 2.Opening Files for Writing Two 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 Moore 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);

J. Michael Moore 3.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);

J. Michael Moore 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);

J. Michael Moore Example: Copying a File

J. Michael Moore 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];

J. Michael Moore 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.');

J. Michael Moore 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 *)

J. Michael Moore Writing To A File: Putting It All Together (4) writeln('Completed reading and writing to files.'); close(letterGrades); close(gradePoints); end.

J. Michael Moore 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); close(inputFile); end;

J. Michael Moore 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); close(inputFile); end; First readln: Character read from file

J. Michael Moore 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); close(inputFile); end; Second readln: runtime error (read past the end of the file)

J. Michael Moore 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.

J. Michael Moore 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);

J. Michael Moore 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; writeln(‘Closing file ‘, inputFilename); close(inputFileVariable); end.