Download presentation
Presentation is loading. Please wait.
1
Computer Science II First With files
2
Problems “I have to type in the same data every time to test my program!” “I want to save my top scores on my home made Zelda the Wind Waker game, but I don’t know how to do it.” “That valentines program really doesn’t make sense if I have to enter all of the information every time some starts the program.”
3
Files Pros Can save information. Dynamic size
Can take my information and have someone else use it in their program. Cons More difficult to code. Sequential access Can erase an entire file with a slip of a command. Did I mention they were more difficult to code?
4
Goals for today Be able to create, save, and load a file.
Be able to implement a file into a past program. Understand the pro and cons of files, especially as compared to arrays. Start understanding the commands associated with using files.
5
Declaring Files: Typed Files, Binary Files, …
String25 = string[25]; Numberfiletype = file of integer; rectype = record name:string25; age:integer; gpa:real; end; recfiletype = file of rectype; var numberfile :numberfiletype; recordfile : recfiletype;
6
Pascal 2 File commands: Assign(Filename, '\\athena.root\Staff\smith_greg\documents\stuff.dta‘); “Makes a synonymous relationship between the file variable Filename and the file on the disk ‘Diskname.dta’. “ Assign ties the variable in your Pascal program Filename to the file on the disk that will be called '\\athena.root\Staff\smith_greg\documents\stuff.dta‘ Diskname.dta. The name of the new file you will soon create. Filename must be declared as a file in the type and var section of the program. This is how you will refer to the file from this point on in the program. The assign command must occur before you open the file or add to the file. Your directory structure could be different. Check when your program compiles. Only ‘stuff.dta’ will work, but the file will not be in your folder.
7
NO SPACES (or punctuation) ex.: These are OK stuff.GSS more.inf
‘Diskname.dta’ is a string defining where the information will be saved. This name has 8.3 file naming rules. 1 <= Number of characters <= number of characters <= 3 NO SPACES (or punctuation) ex.: These are OK stuff.GSS more.inf address.bok These are no good LotsOfinformationabouttheDataICollectedDuringComputerScienceTwoClass.Today My data.dta
8
NOTE: THE ASSIGN COMMAND MUST OCCUR BEFORE USING ANY OF THE FOLLOWING COMMANDS!
IT ONLY NEEDS TO OCCUR ONCE.
9
rewrite(Filename); This creates the file. On the disk this will end up erasing any file of the same name as defined in the assign command above. It does this by opening the file and putting the file POINTER to the beginning of the file. If the file did not exist before this command, this command brings it into existence, if it did exist, it will be written over.
10
Write(Filename, Shuttle);
This copies the information in Shuttle to the file (at the position of the file pointer) and moves the file pointer to the next position. Filename is declared in the type and var part of the program as a file of Sumthin. The shuttle must be of type Sumthin or you’ll get a type mismatch. Sumthin can be a simple type integer, real, char, boolean, string or a defined type, record, array, String25, …
11
close(Filename); When the file is opened with the rewrite command this puts an EndOfFile marker at the position of the pointer and detaches the pointer from the file.
12
Do you remember? How do you declare a file?
How do you attach a file variable to a file on the computer? What command actually starts the file? How do you put information into the file? What command do you need to use when you are finished putting information into the file?
13
Reset(Filename); This opens an existing file. If the file does not exist, the program will crash here. We’ll see how to check if the file exists later.
14
Read(Filename,shuttle);
This copies the information from the file at the position of the pointer to the variable shuttle and moves the file pointer to the next position. NOTES: This is an error if you try to read the EndOfFile marker. This is also an error of there is nothing to read. Shuttle must be of type Sumthin and must be a variable.
15
Close(Filename); This detaches the pointer from the file when the file was opened with the reset command. Seek(Filename,Position); This moves the file pointer to the position declared by Position. The file must be opened with the Reset(Filename) command before Seek can be used. What possible benefits and problems can this cause?
16
NumberVariable:=FileSize(Filename);
This is a function that returns the size of the file. This size is measured by the number of elements (Sumthin’s) in the file. BooleanVariable:= EOF(Filename); This is a boolean (T/F) function that returns a true if the file pointer is pointing to the EndOfFile marker and it returns false if the file pointer is not pointing to the EndOfFile marker.
17
Sample Program Program filesample; type filetype = file of integer;
var shuttle:integer; Numfile :filetype; count,total:integer; begin
18
assign(Numfile,’p:\cs2\number.dta’);
rewrite(Numfile); writeln(‘Enter a number -1 to quit’); readln(shuttle); while (shuttle <>-1) do begin write(Numfile,shuttle); {This puts the information into the file.} writeln(‘Please enter a number -1 to quit’); end; close(Numfile);
19
{Showing from the file}
writeln(‘These are the numbers from the file’); reset(Numfile); while not EOF(Numfile) do begin read(Numfile,shuttle); writeln(shuttle); end; close(Numfile);
20
{Showing from the file using a for..do loop}
writeln(‘These are the numbers from the file’); reset(Numfile); for count:= 1 to filesize(numfile) do begin read(Numfile,shuttle); writeln(shuttle); end; close(Numfile);
21
end. {Totaling the scores in the file} total:=0;
for count:= 1 to filesize(Numfile) do begin read(numfile,shuttle); total:= total+shuttle; end; close(numfile); writeln(‘The total of the number is ‘, total); end.
22
File review Declare Assign(Numfile, ‘p:\cs2\stuff.dta’);
Type Filetype = file of integer; Var Numfile:Filetype; Ties the file variable to the file on the disk. Creates (erases) a file to be written to. Puts information into a file. Puts an EOF marker in the file if adding to the file, and releases the file pointer. Opens a pre-existing file Checks for the end of the file Returns the number of ‘chunks’ in the file. Moves the file pointer to the ‘Position’ spot on the file Used to copy information from a file. See above. Declare Assign(Numfile, ‘p:\cs2\stuff.dta’); Rewrite(Numfile); Write(Numfile, variable); Close(Numfile); Reset(Numfile); Eof(filename) FileSize(Filename) Seek(Filename,Position); Read(Numfile,Variable);
23
Dry Run Fun reset(fyle); while not eof(fyle) do program fileOFun; type
begin read(fyle, shuttle); total:=total+shuttle; writeln(shuttle, total); end; close(fyle); readln; End. program fileOFun; type filetype = file of integer; var Shuttle, total:integer; fyle:filetype; count:integer; begin assign(fyle,’p:\cs2\stuff.dta’); total:=0; rewrite(fyle); for count:= 12 to 14 do Shuttle:= count*2; write(fyle,shuttle); end; close(fyle);
24
Matching ____EOF() a) A type of variable that saves information to a disk. ____Dry run b) A type of variable is used to store someone’s name. ____ close() c) A command used to create a file 4) ____ reset() d) A function used to find the number of elements in a file. 5) ___ read() e) A function that is FALSE when you are not at the end of a file. 6) __ rewrite f) A command used to tie a file type variable to a file on a disk. 7) __ string g) A command that will place an ‘end-of-file’ marker in the file. 8) __ File h) A command used to send information to a file. 9) __ filesize() i) A command used to get information from a file. 10) ___ write() j) Writing what a program prints. 11) ___ assign() k) A command used to open a file for reading.
25
Write the type and var declarations for files to store the following.
The names of the students in class. The ages for the students in class. The song name, song length, and artist for each song.
26
Dry run the following program fileOfStuff; {5 pts} type RecordType= record a,b:integer; c:real; end; filetype = file of Recordtype; var Shuttle:Recordtype; fyle:filetype; count:integer; begin assign(fyle,'stuff.dta'); rewrite(fyle); for count:= 10 to 13 do Shuttle.a:= count DIV 2; Shuttle.b:= count mod 3; Shuttle.c:= (shuttle.a+shuttle.b)/2; write(fyle,shuttle); close(fyle); reset(fyle); while not eof(fyle) do read(fyle, shuttle); if shuttle.a mod 2 = 0 then writeln(shuttle.a:5, shuttle.b:5, shuttle.c:6:2); readln; End.
27
First File Program 1) Input an unknown number of names into a file.
Show them from the file. 2) Input: An unknown number of integers. Process: Using a file for storing the information, find the total, average, and the number of values within 3 of the average of the values. Output: A chart of the numbers entered. The average The number of values within three of the average. Example: If you enter: 3, 10, 17, 12, 8 Numbers: 3 10 17 12 8 The average = 10 The number within 3 of the average = 3 3) Add a save and load option to the life program.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.