250 likes | 269 Views
CPSC 110 - Pascal. Brent M. Dingle Texas A&M University Chapter 12 – section 1. Chapter 12. For this chapter we will really only be concerned with the first section – though we may touch on some of the other sections. Chapter 12.1 – stuff to know. assign reset rewrite append close
E N D
CPSC 110 - Pascal Brent M. DingleTexas A&M University Chapter 12 – section 1
Chapter 12 • For this chapter we will really only be concerned with the first section – though we may touch on some of the other sections.
Chapter 12.1 – stuff to know • assign • reset • rewrite • append • close • read/readln, write/writeln • eof • eoln
Before the new stuff • Before getting into the new stuff be certain you understand how the commands write, writeln, read and readln work. • We are about to add some functionality to them. • Effectively they will work as you know them to, but they will be writing or reading from a file instead of to the screen or from the keyboard.
Files for output • Sooner or later your TA’s are going to get tired of just reading your program’s output on the screen and will want it put in a file. • Fortunately, with very little modification, Pascal will allow you to output stuff to a file instead of to the screen.
New TYPE of variable • The secret to outputting text to a file is the variable type TEXT. • This works just like other variables in declaring it:VAR file_ref : text;
Associating a variable with a file on the disk. • So once you have a text variable you need to associate with a filename on your disk drive:. • Use assign to do this. • Say the filename was:data.txt • To associate the variable file_ref with data.txt you would type:assign(file_ref, ‘data.txt’); • Can you include a path with the file name? • Try it and see!
3 Options • Once a text variable is associated with a file, you need to say what you plan to do with the file. • You have 3 options: • Read the file. • Write (overwrite) the file. • Append stuff to the file (i.e. add stuff at the end).
Preparing to write (overwrite) a file • To PREPARE to write to the file named data.txt you would need a variable of type text declared and TWO lines of code:VAR file_ref : text : :BEGIN assign(file_ref, ‘data.txt’); rewrite(file_ref); : :
Actually writing stuff • To actually write to a file is easy, you just use write or writeln – but with an additional parameter. • So if you wanted to write a line of text to the file referenced by file_ref you would type:writeln(file_ref, ‘This line is written to a file’);
Ending writing • Before your program ends you MUST close the file you were writing to. • If you do not close the file, some data may be lost – it doesn’t get written to the file. • To close the file add this line before the ’END.’ of your program:close(file_ref);
PROGRAM WriteStuff; CONST BLANK = ‘ ‘; VAR file_ref : text; n : integer; BEGIN writeln(‘Program running…’); assign(file_ref, ‘data.txt’); rewrite(file_ref); writeln(file_ref, ‘Howdy’); writeln(file_ref, 3, BLANK, 4); n := 5; writeln(file_ref, n); close(file_ref); writeln(‘… end program’); END. Writing to a FileProgram Example
Program Result • The above program WriteStuff would create or overwrite the filenamed data.txt. • Usually you will specify a path with the file name. • For example if you wanted data.txt created on the root directory of the a: floppy disk you would changeassign(file_ref, ‘data.txt’);to:assign(file_ref, ‘a:\data.txt’);
Appending to a file • Appending is similar to writing – except anything you write or writeln will be tacked onto the end of the file, rather than overwriting what was previously in the file. • There is one difference and that is in the preparation to append to a file .
Preparing to Append to a file • To PREPARE to append to the file named data.txt you would need a variable of type text declared and TWO lines of code:VAR file_ref : text : :BEGIN assign(file_ref, ‘data.txt’); append(file_ref); : :
Appending – Close the file • When you are done appending to the file… • As with writing to a file, you must remember to close the file:close(file_ref);
Preparing to Read a file • To PREPARE to read the file named data.txt you would need a variable of type text declared and TWO lines of code:VAR file_ref : text : :BEGIN assign(file_ref, ‘data.txt’); reset(file_ref); : :
Actually reading stuff • To read something from a file is pretty easy – just use read or readln. • Of course you need to make sure your type of variables are set correctly and your file is in the correct format.
Before our example • Before you can read a file you need to know at least: • if it exists • what format its contents are in • Assume we have a file named stuff.txt that contains the following:Si73 • Now we want a program to read that file and display its contents on the screen…
PROGRAM ReadStuff; VAR in_file : text; first_letter, second_letter : char; n1, n2 : integer; BEGIN assign(in_file, ‘stuff.txt’); reset(in_file); writeln(‘Program begins…’); readln(in_file, first_letter, second_letter); writeln(first_letter, second_letter); readln(in_file, n1); readln(in_file, n2); writeln(n1); writeln(n2); close(in_file); writeln(‘… end program’); END. Reading to a FileProgram Example
Read Program Result • Assuming all went well, the ReadStuff program above would output:Si73 • Notice we had to be certain to read our variables in the correct order so their type matched that of what was being read. • Could we have made everything of type char? • Try it and find out !
Reading – Close the file • When you are done reading from the file… • As with writing and appending to a file, you must remember to close the file. Notice near the end of the above program was the statement:close(in_file);
Other stuff • eof(file_ref) • Is used to check if you have reached the End Of the File. • eoln(file_ref) • Is used to check if you have reached the End Of the Line. • eof tends to be used more than eoln. • A program example using eof should be found in the sample program list – the reading a file example.
Near the end • Well, that is all I’m going to explicitly talk about in Chapter 12. • You may wish to read more about assign, eof, eoln, reset, rewrite and append as they may be useful in some of your assignments. • I would encourage you to try doing Exercises 3, 4, 5 and 6 on page 471.Who knows… maybe one of them might be an assignment later… =)