By using the File Reader Virtual Block you can input data to manipulate it. The File Reader can read text files one character at a time. The example below shows how to configure the file reader to receive data from a file.
Example: Read from a file
- First prepare the file you want to be read. Create a text file in Notepad and save it for later use.
- Insert the File Reader Block in to the Workspace by clicking on it in the Quick Menu.
- The block will appear in your Workspace ready to configure. Click on the block to open its Properties Panel.
- From the Properties Panel select the file button to open the text file you would like to read.
- Once you have selected your file, you should see the name of the file, and size will appear in the Properties Panel.
If you notice, the Total Bytes matches the number of characters in the file.
- Open the Logic Maker.
- Insert the following Gates in the Logic Maker. INPUT>DISPLAY PLAY>ACTION
- Connect up the gates following the image below:
- Select Read from the ACTION Gate drop down menu.
- Select Bytes from the INPUT Gate.
- Click Apply in the logic maker.
- Now click the PLAY Gate to start reading from the file.
Each time you click the PLAY Gate, the INPUT Gate will receive a byte from a file. As you can see, the numbers received do not correspond to the numbers that were written in the file. This is because the file is read using ASCII numbers. ASCII allows for keyboard characters to map to numbers, so that it is easier for the computer to store the character in memory.
If you look up a table of ASCII codes you will be able to see the corresponding ASCII code for each of the keys on your keyboard. The table below shows the ASCII codes for numbers 0-9.
ASCII- Decimal | Character |
48 | 0 |
49 | 1 |
50 | 2 |
51 | 3 |
52 | 4 |
53 | 5 |
54 | 6 |
55 | 7 |
56 | 8 |
57 | 9 |
For simplicity numbers were used, however you can use the file reader to read any character that you are able to type/paste into a text file.
INPUT Gate Options
BYTES
This option will receive the byte created from the ACTION Gate when it is set to "Read". Each time the "Read" is triggered an event is produced containing the byte that was read.
FINISHED
An event is created when the Read ACTION Gate reaches the end of the file.
ACTION Gate Options
READ
This option will trigger the reading of one byte. Once all bytes are read in the file it will loop back to the beginning of the file and continue on.
RESET
By triggering the ACTION Gate in Reset mode, it will reset the position it was reading from, and start from the beginning again.
MOVE
By using the Move option, you are able to specify where to start reading from. Use a VALUE Gate to send the position to start. (Note: the indexing starts at 0)