70 likes | 275 Vues
Pascal Programming . Files and Text (an intro). Pascal Programming. Files . . . Data files need to be retained in secondary memory. The machine memory is random access (RAM) and volatile—it is erased by turning off the machine.
E N D
Pascal Programming Files and Text (an intro)
Pascal Programming • Files . . . • Data files need to be retained in secondary memory. • The machine memory is random access (RAM) and volatile—it is erased by turning off the machine. • Secondary memory could be the hard disk drive, a removable disk or a R/W CD ROM.
Pascal Programming • Thus, file can be defined as a collection of data, named and stored. • In DOS, the file naming convention is 8/3 or XXXXXXXX.XXX. • File being processed by Pascal have a name for storage and a file variable name. • A file variable must be declared.
Pascal Programming • File variables . . . • . . .cannot be used in assignment statements. • . . .have predefined procedures for processing. • . . .have predefined syntax. • assign – the predefined procedure, calls the file. • Assign (File, ‘data.txt’) • This is the first step in working with a saved file.
Pascal Programming • To read or write to a file, it must be opened. • assign (File, ‘data.txt’); • rewrite (File); • Write and writeln statements write to the file. • writeln (File, ‘new data’) • Remember, File is a file variable name and not a variable to be written out. • After use, files must be closed. • close (File) • To add to a file, append. • append (File)
Pascal Programming • Printing . . . • Turbo Pascal writes to the printer like it writes to a file. • e g: • assign (File, ‘PRN’); • rewrite (file); • writeln (File, ‘data.txt’); • close (File);
Pascal Programming • reset opens a file for reading. • assign (File, ‘data.txt’); • reset (File); • read and readln will read the content of the file. • The eof (end of file) function will allow a read statement to proceed to the end of the file. • eof (File)