All Tutorials / JustinRatliff / Reading and Writing with Files

Reading and Writing with Files

Step 7. Making real use of data from a file

If you write to a file "Time Ping 124" you are going to have a very hard time using it in another script. If you separate it with commas so that it is written as "Time,Ping,124" it is going to make it easier for a script to read that line of data and make sense of it. Because you could script reading data after the second comma, or any other place in the line because the data is separate by commas which is what allows a script to read the data in an understandable way.

But be careful how you use the commas. If your file output ends up being "Time , Ping , 124 " those spaces will probably throw off the usability of those values, because while you know the value for the ping sensor is "124", what your robot will read it as is " 124 " which renders the data nearly useless because of the extra spaces before and after the numerical value of 124.

Of course you could simply write ping data like this to a single line with only the ping sensor value, just "124" with nothing else.

Having said all that, let's make sure your file output from "WriteTest3" script is clean with a slight modification. Lets clean up our script with the FileDelete line add back. Modify the WriteTest3 script as shown, save it and run it.


$filename = "c:\temp\myfile2.txt"

$PingValue = 124
FileWriteLine($filename,"Time:," +$time + ",Ping," + $PingValue)


After running the script, open the "myfile2" and make sure it looks like this:
User-inserted image

If it does, close the file. If it does not, review the previous steps to see what you might have missed.

There is a sample script named "ReadFields" that we could use to see how to read comma separated text files, but I'm going to show you a slightly different example. Add a new script control and name it "ReadCommaFile" and enter the following code, then save the script.


# Reads a file and seperates each field by a comma.
# The Split() function will return a
# specified field index

$filename = "c:\temp\myfile2.txt"

if (FileExists($filename) = false)
print("File does not exist")


$cnt = 1


$txt = FileReadLine($filename)

$TimeField = Split($txt, ",", 1)
$SensorField = Split($txt, ",", 2)
$SensorValueField = Split($txt, ",", 3)

print("Row: " + $cnt)
print("Time: " + $TimeField)
print("Sensor: " + $SensorField)
print("SensorValue: " + $SensorValueField)

if (FileReadEnd($filename) = true)
print("End Of File")

$cnt = $cnt + 1



This reads the line of data from our file and separates out the data by commas so it can be used. The data in our file was:


Going by comma position, the data breaks down as follows:
Time: = 0
16:35:54 = 1
Ping = 2
124 = 3

So the line: $SensorValueField = Split($txt, ",", 3) is reading the data after the 3rd comma. And $SensorValueField is the variable we are using to store that value.

Now you can use this value elsewhere in other scripts if you need to. Lets add a new Script control and name it "NearOrFar", then enter the following code and save the script.


if ($SensorValueField > 100)
Print("I was far away from something.")
Print("I was near something.")

Run the script. Based on the value being 124, the script should print "I was far away from something.".

Now go back to your "WriteTest3" script and change the value from 124 to 80. Save the script and run it.

Now run the "ReadCommaFile" script again.

Now run the "NearOrFar" script again. This time the script should print "I was near something.".