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.

Slides:



Advertisements
Similar presentations
Introduction to File I/O How to read & write data to a disk file...
Advertisements

James Tam Linked Lists in Pascal Linked Lists In this section of notes you will learn how to create and manage a dynamic list.
J. Michael Moore Text Files CSCE 110 From James Tam’s material.
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 Getting Started With Pascal Programming How are computer programs created What is the basic structure of a Pascal Program Variables in Pascal.
James Tam Arrays In this section of notes you will be introduced to a homogeneous composite type, one- dimensional arrays.
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 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.
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.
1 11/3/08CS150 Introduction to Computer Science 1 Reading from and Writing to Files Section 3.12 & 13.1 & 13.5.
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 Arrays CSCE 110 From James Tam’s material.
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 Getting Started With Pascal Programming How are computer programs created What is the basic structure of a Pascal Program Variables and constants.
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 Getting Started With Pascal Programming What is the basic structure of a Pascal Program Variables in Pascal Performing input and output with.
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.
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.
James Tam Breaking Problems Down This section of notes shows you how to break down a large problem into smaller modules that are easier to implement and.
Getting Started With Pascal Programming
J. Michael Moore From James Tam's material Records CSCE 110.
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 Getting Started With Pascal Programming How are computer programs created What is the basic structure of a Pascal Program Variables and constants.
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.
James Tam Records You will learn in this section of notes what is a record and how to use them in Pascal.
James Tam Pointers In this section of notes you will learn about another type of variable that stores addresses rather than data.
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 Breaking Problems Down This section of notes shows you how to break down a large problem into smaller modules that are easier to implement and.
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 Pointers In this section of notes you will learn about another type of variable that stores addresses rather than data.
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)
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.
Pascal Programming Today Chapter 11 1 Chapter 11.
CS241 PASCAL I - Control Structures1 PASCAL Control Structures Modified Slides of Philip Fees.
24-2 Perform File I/O using file pointers FILE * data-type Opening and closing files Character Input and Output String Input and Output Related Chapter:
GAME203 – C Files stdio.h C standard Input/Output “getchar()”
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.
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
IGCSE 4 Cambridge Data types and arrays Computer Science Section 2
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.
Introduction To Files In Python
Sorting (Bubble) In this section of notes you will learn one technique for ordering a list.
Arrays In this section of notes you will be introduced to a homogeneous composite type, one-dimensional arrays One-dimensional arrays in Pascal.
Topics Introduction to File Input and Output
Topics Introduction to File Input and Output
Computer Science II First With files.
Presentation transcript:

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 What You Know About Input And Output Comes from the user or is displayed to the user Person to program (read / readln) Person to program (write / writeln)

James Tam What You Will Learn: 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)

James Tam Why Bother With Files? Too much information to input all at once The information must be persistent (RAM is volatile) Etc.

James Tam What You Need In Order To Read Information From A File 1.Declare a file variable 2.Open the file 3.A command to read the information

James Tam 1.Declaring File Variables Allows the program access to a file Format: name of file variable : text; Example: letterGrades : text;

James Tam 2.Opening Files 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); Example: (Both) var letterGrades : text; (Constant file name) reset (letterGrades, ‘letterGrades.txt’); OR (Variable file name) var filename : string [80]; readln(filename); reset(letterGrades, filename);

James Tam A.Linking The File Variable With The Physical File Your program : File variable (letterGrades) :::::: letterGrades.txt (text file)

James Tam B.Positioning The File Pointer ABCBB:ABCBB: letterGrades.txt

James Tam 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);

James Tam 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 *) Example: while NOT EOF (letterGrades) do begin readln(letterGrades, letter); writeln(letter); end; (* Loop to read letter grades file *)

James Tam An Alternative Approach To Reading Files Employ a sentinel in the file Keep reading from the file until the sentinel value is encountered Example: var inputFile: text; var num: integer; : : readln (inputFile, num); while NOT (num = -1) do begin writeln(num); readln(inputFile, num); end; (* Done reading input file *)

James Tam Reading From Files: Putting It All Together A complete version of this program can be found in Unix under /home/231/examples/files/grades.p: program grades (output); const FILENAME_LENGTH = 256; begin var letterGrades : text; var letter : char; var inputFile : string[FILENAME_LENGTH]; write('Enter name of input file: '); readln(inputFile); reset(letterGrades, inputFile); writeln('Opening file ', inputFile, ' for reading.'); while NOT EOF (letterGrades) do begin readln(letterGrades, letter); writeln(letter); end;

James Tam Reading From Files: Putting It All Together (2) close(letterGrades); writeln('Completed reading of file ‘, inputFile); end.

James Tam View Of Files In Unix home 231 examples files grades*letterGrades.txtgrades.p \

James Tam What You Need To Write Information To A File 1.Declare a file variable 2.Open the file 3.A command to write the information

James Tam 1.Declaring An Output File Variable 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;

James Tam 2.Opening The File Two methods: 1)Rewriting – erases the old contents of the file (rewrites over what was already there). 2)Appending – retain the old contents of the file (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’);

James Tam Opening The File (2) Example (rewriting / appending): (Variable file name) var outputFile : string[80]; var gradePoints : text; rewrite(gradePoints, outputFile); append(gradePoints, outputFile);

James Tam 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);

James Tam Writing To A File: Putting It All Together A complete version of this program can be found in Unix under: /home/231/examples/files/grades2.p program grades (output); const FILENAME_LENGTH = 256; begin var letterGrades: text; var gradePoints: text; var letter: char; var gpa: integer; var inputFile: string[FILENAME_LENGTH]; var outputFile: string[FILENAME_LENGTH];

James Tam Writing To A File: Putting It All Together (2) write('Enter the name of file that contains the letter grades: '); readln(inputFile); write('Enter the name of the file to store the grade points: '); readln(outputFile); writeln('Opening file ', inputFile, ' for reading.'); writeln('Opening file ', outputFile, ' for writing.'); while NOT EOF (letterGrades) do begin readln(letterGrades, letter);

James Tam Writing To A File: Putting It All Together (3) 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 *)

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

James Tam Details Of Write And Writeln For Files: Intuitive View Program statementEffect on file rewrite(data,’data.txt’); (Open file "data.txt" and position file ^ pointer at start) write (data, 'x');x ^ write(data, 'y');xy ^ write(data, 'z');xyz ^ writeln(data);xyz _ ^ write(data,'a');xyz a ^

James Tam Details Of Write And Writeln For Files: Actual View Program statementEffect on file rewrite(data,‘data.txt’); (Open file "data.txt" and position file ^ pointer at start) write (data, 'x');x ^ write(data, 'y');xy ^ write(data, 'z');xyz ^ writeln(data);xyz ^ write(data,'a');xyz a ^

James Tam Details Of Read And Readln For Files: Intuitive View 1 Program statementEffect on fileEffect in program reset (data, ‘data.txt’);xyz(Open file ^ "data.txt" and position file pointer at start) a read(data, ch);xyzValue of ch: 'x' ^ a readln(data, ch);xyzValue of ch: ‘y’ a ^ read(data, ch);xyz Value of ch: 'a' a ^ 1 Assume that the code on the previous slide has created the file called "data.txt"

James Tam Details Of Read And Readln For Files: Actual View 1 Program statementEffect on fileEffect in program reset (data, ‘data.txt’);xyz a(Open file ^ "data.txt" and position file pointer at start) read(data, ch);xyz a Value of ch: 'x' ^ readln(data, ch);xyz a Value of ch: 'y’ ^ read(data, ch);xyz a Value of ch:'a' ^ read(data,ch);xyz a ^ 1 Assume that the code on the previous slide has created the file called "data.txt"

James Tam Details Of Read And Readln For Files: Actual View 1 Program statementEffect on fileEffect in program reset (data, ‘data.txt’);xyz a(Open file ^ "data.txt" and position file pointer at start) read(data, ch);xyz a Value of ch: 'x' ^ readln(data, ch);xyz a Value of ch: 'y’ ^ read(data, ch);xyz a Value of ch:'a' ^ read(data,ch);xyz aError: read past ^ the end of the file 1 Assume that the code on the previous slide has created the file called "data.txt"

James Tam 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 pointer at a character in the file.

James Tam Explicitly Dealing With Empty Files: An Example A complete version of this program can be found in Unix under: /home/231/examples/files/fileExampleThree.p 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);

James Tam 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.

James Tam Passing File Variables As Parameters Must be passed as variable parameters only. Format: procedure nameProcedure (var nameFile : text); Example: procedure fileInputOuput (var letterGrades : text; var gradePoints : text);

James Tam You Should Now Know How to declare a file variable How to open a file for reading How to open a file a file for writing (rewrite and append mode) How to read (read/readln) from and write (write/writeln) to a file The details of how information is read from and written to a file How to close a file and why it is good practice to do this explicitly How to explicitly deal with empty files How to pass file variables as parameters