Current Notes May 1987 USING KYAN PASCAL A Short Tutorial by Alan Friedman Now that everyone knows how good the improved Kyan Pascal is it's time to learn how to use it. The tutorial that came along with the disk is very good, but falls short in the area of text file and record handling. What we are going to be doing here is going over some of the basics covered in the manual and expanding on them with examples and descriptions. The first area that needs to be covered is how to create a text file. This is actually very easy. After booting up the disk on side 1 type in ED after the % symbol appears. You will then be asked for the name of the file to be loaded. Type in the name you want your file to be called. The screen will say file not found press any key to continue. After pressing a key the screen will be blank with a square cursor blinking in the upper right corner. You are now in the word processor. It is here you create your programs and can also create text files. By simply typing in your text, making sure to press the return key at the end of each line you are creating a text file the compiler can manipulate and change later. When finished press the Esc key and and then the X key for save and exit. You have now created and save a text file. Now we must access the file. To do this we need to declare a variable to say we are going to access a file. I like to use the name "InFile" to indicate I will be reading from this file. InFile will be of the standard type TEXT. TEXT is predefined in Pascal as strings of characters. So under the VAR heading we have: InFile:TEXT; We also need to declare a variable to hold the character we want to read until it is processed. This variable will be Ch and will be of the type char (Character); Ch:CHAR; Our program should now look like this: PROGRAM ReadCh(Input,Output); VAR InFile:TEXT; Ch:CHAR; The First line says this is a program named ReadCh and that we will ____________________________________________________________________ XL - 1 - XE Current Notes May 1987 be entering data and printing data out. Notice that each line ends with a semi-Colon(;). The semi-colon ends an assignment statement. The text says that you do not use a semi-colon before an END statement, but this is not true. You can use a semi-colon before an end statement in all cases except after an END that preceeds an ELSE. It is a good idea to use a semi-colon wherever possible since this will eliminate problems if you have to change a proram later. The next line of our program is the execution word BEGIN. Now that we have begun the program we need to tell it where to get the data from. This is done with the Reset command. Reset(InFile,'YourTextFileName'); This tells the computer that you will be reading from an input file and resets it to the beginning of the file. Of course, where I have YourTextFileName is where you would put the name you gave your file. Don't forget that the quotation mark in Pascal is a single apostrophe. We now have to tell the computer where to stop reading the text file. What we will do is actually tell it to keep reading the file as long as we have not reached the end of file. The end of file marker is added by the editor when you saved your text file. We can accomplish this with: WHILE NOT EOF(InFile)DO This simply says as long as we have not read the EOF marker for the input file continue doing whatever follows. After this we need another BEGIN statement. When there is a command like WHILE..DO, or IF..THEN the computer will automatically do whatever is on the next line. If you need to do more than one process then you must enclose everything you need to done with a BEGIN and END statement. You can have several functions and begin and end statements nested inside of each other. Next we need to tell the computer to continue the process we are going to set up until we reach the end of the line. This is almost the same as the EOF line and is as follows: WHILE NOT EOLN(InFile)DO BEGIN Now we need to tell the computer what to do next and that is to read a single character and print it to the screen. Read(InFile,Ch); Write(Ch); We are now reading a character from the text file and printing it to the screen. We will do this until we reach the end of the line. We do this with the END statement for the BEGIN of the WHILE NOT EOLN(InFile) ____________________________________________________________________ XL - 2 - XE Current Notes May 1987 So now our program looks like this: PROGRAM ReadCh(Input,Output) VAR InFile:TEXT; Ch:CHAR; BEGIN WHILE NOT EOF(Infile)DO BEGIN WHILE NOT EOLN(InFile)DO BEGIN Read(InFile,Ch); Write(Ch); END; At this point we have read all the characters on the line and reached the end of line marker. Now we must clear out the rest of the line we are reading of all markers and begin reading the next line as long as we are not at the end of the file. We also need to print a carriage return to begin printing the next line. Readln(InFile); Writeln; will accomplish this. Remember the read statement reads only a single character whereas the readln statement reads the entire line. The same can be said about write and writeln. Write writes only a single character and wariteln will write a character or more and will cause a line feed and carriage return to occur. If we have gone all the way through the file then the computer will read an end of line marker and the next character it will read is the end of file marker. All that is left to do to our program is write the END statements for the begins that are associated with the WHILE NOT EOF(Infile) and the beginning of the program itself. The entire program is: PROGRAM ReadCh(Input,OutPut) VAR InFile:TEXT; Ch:CHAR; BEGIN WHILE NOT EOF(InFile)DO BEGIN WHILE NOT EOLN(InFile)DO BEGIN Read(InFile,Ch); ____________________________________________________________________ XL - 3 - XE Current Notes May 1987 Write(Ch); END; END; END. Notice that in structured programming how we indent everything between a BEGIN and an END. This way we can see everything that happens in each section. The reason we used a WHILE statement instead of using UNTIL is that we wanted to check for the EOF and EOLN markers before we read a character. This is because these markers can almost be considered magical. We can look at them and check to see if they are there all we want to but if we read them they turn to space and disappear. So if we did a Read(InFile,Ch) and then an IF EOLN or UNTIL EOLN and the character we read was a EOLN or EOF then they would be printed as a space and they would be gone and our system would make many mistakes because we would miss the EOLN markers and would then crash if we read over the EOF marker. Okay, so what do we have now? We have a program that reads a text file and prints it to the screen. Can we improve it easily? Yes by adding two lines and changing one we can print the file to the printer. First declare a variable printer that is also of the type text: Printer:TEXT; This goes right after InFile:TEXT; After the line Reset(InFile,XXXXXX); add the line: Rewrite(Printer,'P:'); This tells the computer that whenever it sees the word 'Printer' send the information to the printer. Finally change the line Write(Ch); to Write(Printer,Ch); This sends each character read to the printer. ____________________________________________________________________ XL - 4 - XE ler operates at a fairly fast speed and allows the user th