codemorphis.com Forum Index codemorphis.com
Software development: pure and simple.
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Programming Tip #5: Reading ASCII Text Files

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    codemorphis.com Forum Index -> Visual Programming Forum
View previous topic :: View next topic  
Author Message
CodeMorphis



Joined: 02 Dec 2003
Posts: 252

PostPosted: Thu Jan 22, 2004 4:31 pm    Post subject: Programming Tip #5: Reading ASCII Text Files Reply with quote

Programming Tip #5: Reading ASCII Text Files


In this programming exercise, we demonstrate through a simple example, how to read text from an ASCII file.

An ASCII file is just a plain text file. An example of an ASCII file is a file with the extension ".txt" which is created by the Microsoft Notepad program.

We will use the Synopsis "Ascii File" component to create the program below:



The Synopsis VPD (Visual Program Description) file, which provides the source for this program can be download here http://www.codemorphis.com/articles/tip5/Token_Reader.vpd


  1. To create this program from scratch, open the component tray in Synopsis and drag and drop the following components onto the workspace area:

    • The "Ascii File" component from the "File" tab
    • The "Service Call" component from the "Program" tab
    • The "If" component from the "Logic" tab
    • The "Console Print" component from the "I/O" tab

  2. Select the "Service Call" component and press Ctrl-C and then Ctrl-V to copy and paste the component.
  3. Double click one of the "Service Call" components and in the Component Properties dialog that appears set the "Object component" property to "Ascii File 1". Select the "End of file" service in the "Active service" property. Click OK to close the dialog.
  4. Double click the second "Service call" component and in the Component Properties dialog that appears set the "Object component" property to "Ascii File 1". Select the "Get token" service in the "Active service" property. Click OK to close the dialog.
  5. Double click the "Console Print" component and set the "Number of print arguments" property to 3.
  6. Connect the rest of the components as in the program snapshot above.
  7. Right click the "Console Print" component and select the "Input Argument(s)" option. Set the value of "Print argument 0" to "Token: ". In the "Print argument 2" value textbox, press the Ctrl button and while holding down the Ctrl button, press the Enter button. This enters a carriage return code. You could accomplish the same thing by double clicking in the "Print argument 2" value textbox and then in the "Data Port Argument Editor" window that appears, simply press the Enter button and then click OK.
  8. Now we need a text file that we will use with our program. Let's create a simple ASCII file with the Microsoft Notepad application. Start the Notepad application and type in some text like this:



  9. Save the Notepad file. In the program example, we have saved it as c:\textfile.txt.
  10. Double click the blue input data port on the Ascii File component (the component named "Ascii File 1" with the service "Open"). Click on the "..." browser button and navigate and select the text file that you just saved from Notepad.

Now press the Run button in the Synopsis toolbar. The contents of your Ascii text file should appear in the console output area and look like:




What did this program do?

  • The "Ascii File" component opens our Notepad created text file for processing. Note that the service selected is "Open" which is used to open an Ascii file.
  • We then enter into a process loop controlled by the "Service Call" with the "End of file" service. The "End of file" service checks whether or not we are at the end of the Ascii file, i.e. if there is no more text to be read in. The result of this service call is either "TRUE" or "FALSE": true if we have reached the end of the file and have no more text to read, otherwise false.
  • This end of file result is passed to the "If" component. If the value is "TRUE" then process control (indicated by the red connections) moves through output process port 1. If the value is not "TRUE" (i.e. it is "FALSE"), then process control moves through output process port 0.
  • Process output port 0 of the "If" component is connected to the input process port of the "Console Print" component, which when it receives process control, prints the token read in from the ASCII file to the console output area. From there, we then go back to the "End of file" service call to see if there is more text to be read in.
  • Note that the process output port 1 of the "If" component is not connected to anything. This means that once we detect that there is no more text to be read in, the program will terminate normally.
  • The resulting functionality of the program is that we read in the Ascii text file, one token (word) at a time, and for each token we were able to perform some processing.

NOTE: We opened the text file with the "Open" service call of the Ascii File component but we never explicitly closed the file. We could have used the "Close" service after we detected the end of file status. But the "Ascii File" component automatically closes the file for you when the Synopsis program terminates. If we wanted to open another text file, however, we would have used the "Close" service before using the "Open" service again.
Back to top
View user's profile Send private message Send e-mail Visit poster's website
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    codemorphis.com Forum Index -> Visual Programming Forum All times are GMT - 5 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group