{The index to all of Carl Drott's Pascal examples is at:} { http://drott.cis.drexel.edu/CommandFinder.html } {While we were beginning to learn pascal we assumed that input would come } {from the keyboard and that all output would go to the screen. We know } {from our other computer use that information frequently comes from a file } {that is stored on disk and output is written to a disk file. For example as you } {write programs you save them to the disk and when you want to work on } {them again you retrieve (open) them from the disk.} {} {To read from and write to disk files in Pascal we will use exactly the same } {commands that we have been using for reading and writing; Readln and } {Writeln (and we have read and write although we haven't used them much.) } {Here are the steps:} {1) Set up a variable to represent the file in the program.} {2) Associate the name of the file in the program with the name of the file on } {disk.} {3) Open the file for reading or writing. If you are writing you may cerate a } {new file.} {4) Whenever you use a Readln or Writeln be sure to tell the machine which } {file you are working with} {} {} {1) Set up a variable to represent the file in the program.} {Do this in the var section of the program and make the variable(s) of type } {"text."} {var} {i,j: integer;} {word:string;} {MyExistingFile, MyNewFile: text;} {} {Important: The word "text" may mean a lot of things to you but in Pascal it } {has a special and specific meaning. A variable of type text does not have } {contentents the way a regular variable does. It names a file where the } {contents are or are to be put.} {Important: you can use any word processor to create a text file for this } {program to read BUT you must save that file as "text with line breaks" (use } {save as and look for a list of possible formats, perhaps under options)} program InDemo; {reads from an input file and puts it on a screen in} {an interesting way. Before you run this look at the second FOR and} {see if you can predict what the program does} var word: string; count, I: integer; data: text; {inside the program the file is called data} begin reset(data, 'myin'); {on the disk the file is called myin} for count := 1 to 4 do {we assume there are at least 4 lines in the file} {as you will see later we should always check} { to see if there is something to read} begin readln(data, word); {Note that Pascal will recognize that it is supposed to read from } {the file that it calls data} for I := 1 to length(word) do begin writeln(word); word[I] := '*'; end; writeln(word); end; writeln('end of program'); {readln(word);} {If you are running in Turbo put this readln in to keep the } {text window open until you press return} {There is NO file name in this readln, so the machine assumes reading from the keyboard} end.