Reading Text Data From A File


Let's first start by using the DOS text editor to create a short data file which we will call "PRACTICE". On SiMPLE's command line, type:

          edit practice

[Notice that we used the DOS command "edit" (instead of SiMPLE’s "open" command ) to directly invoke the DOS editor program.] When the familiar blue window appears, type in the following list of integer numbers:

          17
          852
          627
          -49
          123

When you are finished, exit the editor. (Don't forget to select 'Yes' when it asks if you want to save your file.)

Now that we've created our data file, we're ready to create the program that will read it.

In Pro-SiMPLE, all disk files are accessed through "Channels." There are seven channels (designated by the letters A-G) available for use. To read from a particular input file you must first open one of the seven channels and specify the name (or path) to the file that you wish to read. You can then begin reading input from the file. When you are finished, you should close the channel so that it knows that you don't expect to read any more data.

The following Pro-SiMPLE program will read the data file that we created, and display it onto the output screen:

          Pro-SiMPLE
          Text line
          Int eof
          Call open a ("read practice")
          Do
             Call read line a (line, eof)
             If (eof) Break
             Display line
          Loop
          Call close a

The program first invokes the system library's opena task to assign the file named PRACTICE to channel A and open it for reading. (We could have used any of the seven channels.) Then it invokes the readlinea task (in the Do-Loop) to input each line of text from the data file. When the end of the file is reached ("eof" is no longer zero), the program breaks out of the loop and invokes the closea task to close the channel.

If you run the program it should display the same list of numbers that you had previously typed into the editor.




  [ Webmaster | FAQ's | Home Page | Contact Us ]