Download presentation
Presentation is loading. Please wait.
1
Computer Science II Second With files
2
Goals for today Review file commands from last class.
Be able to read a program that uses files Be able to fix a program that uses files Be able to write a program that uses files
3
Dry run the following code
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. Dry run the following code
4
philephix.pas on the class website
program fix’nFile; {This program is supposed to create a file of 1000 random numbers then show them on the screen} type file = file of integer; var total:integer; begin assign(file, ‘numb.skl’); reset(file); for count := 1 to file do readln(file, total); end; for count = 1 to EondOfFile do writeln(file) end. Fix File philephix.pas on the class website
5
File review Declare Assign(Numfile, ‘stuff.dta’); Rewrite(Numfile);
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, ‘stuff.dta’); Rewrite(Numfile); Write(Numfile, variable); Close(Numfile); Reset(Numfile); Eof(filename) FileSize(Filename) Seek(Filename,Position); Read(Numfile,Variable);
6
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
© 2025 SlidePlayer.com. Inc.
All rights reserved.