Welcome to Synthiam!

The easiest way to program the most powerful robots. Use technologies by leading industry experts. ARC is a free-to-use robot programming software that makes servo automation, computer vision, autonomous navigation, and artificial intelligence easy.

Get Started
Asked — Edited

Script Help - Reading From A File


I am looking for help with a script or to help guide me to a script that will allow me to read contents from a saved file and compare to a string value.

I already have a script that will get the value of a servo, and store it to a file. I would like to read that same file and compare the values.



Upgrade to ARC Pro

Get access to the latest features and updates with ARC Early Access edition. You'll have everything that's needed to unleash your robot's potential!

AI Support Bot
Related Content
Based on your post activity, we found some content that may be interesting to you. Explore these other tutorials and community conversations.
After you have created and saved data to your file you can use these commands to retrieve data from the file after you open or re-open it


FileReadReset( filename ) #start your read at the beginning of the file

$contents=FileReadAll( filename ) #read whatever is saved in the entire file

$contents=FileReadLine( filename ) #each call to this reads the next line in succession until the end of the file is reached

So something like this?


#this assumes you have already created and saved your data to a file

#open the file to read

FileReadReset("myfile.txt") #full file path is needed
$contents=FileReadAll("myfile.txt") #read entire file info into $contents

If($contents != $currentValue)
#then do something

FileReadClose("myfile.txt" ) #close the file when done with it
FileDelete("myfile.txt") # delete it if you have no more need for it
So, lets say you stored a value in a test file, to read it you can use


$line = FileReadLine(C:\Users\weyou_000\Documents\EZ-Builder\test.txt)
@RichardR , Thank You, is there a way to read each line in the file and then compare? Variable Watcher has a bunch of numbers all into one string value.
Yes, you can do it like this for simplicity...


$contents1=FileReadLine("myfile.txt") #read line 1
$contents2=FileReadLine("myfile.txt") #read line 2
$contents3=FileReadLine("myfile.txt") #read line 3
$contents4=FileReadLine("myfile.txt") #read line 4

So $contents1 through $contents4 variables store the data of the first 4 lines of data that was in the file respectively...
I then would need to read the file, see how many lines, then create the $contents string variable according to how many lines I have... am I thinking right on this? Or is there a better way?

Maybe I need to read from the file and then make an array then compare all the array entries to the servo value?
If you're the one writing to the file, u shouldn't need to check how many lines are there because u will already know. You're the creator:)

I assume you want to write servo positions to be saved and resumed at a later date?
@DJ Silly Me.. Yes, you are correct!

@WayneA you could save in the file the # of lines it contains... So for instance, the first line in the file could be the number of lines (or entries) that the file contains. That way after opening the file and reading the first line you will now know how many lines are left to read....
Like this...



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


filewriteline($filename, getservo(d0))
filewriteline($filename, getservo(d1))
filewriteline($filename, getservo(d2))
filewriteline($filename, getservo(d3))
filewriteline($filename, getservo(d4))



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


servo(d0, filereadline($filename))
servo(d1, filereadline($filename))
servo(d2, filereadline($filename))
servo(d3, filereadline($filename))
servo(d4, filereadline($filename))

Is there a way to specify a line to over write?
justin, no - files don't work that way. you start a file by deleting it or you append to it by writing to it. computers do not have a "edit the file half way". so you read the whole file in, modify what you want. delete the file, and write the new one
@DJ What would be the script structure to read and write and close the same file multiple times from within one script?
Wayne, why would you need to do it multiple times?

Lets say you had 3 variables you wanted to store in a text file, like servo values or whatever. You'd have a "ReadFileScript" to read the values from the file to give you your base line data.

These would be active current values for the variables.
$value1 = 1
$value2 = 2
$value3 = 3

Then I would have a "WriteFileScript" that would delete the file and re-write the data in the file when needed.

I'd have another script to watch for changes to the values, if they change then kick off the "WriteFileScript".

I don't think you'd want to lump them together and I don't think you'd need to read and write together, you should only need to read the file when the program starts. After that you just want to write data when values change.
@Justin, thanks for the suggestion, however I had hoped that @DJ could answer for me.
Wayne, my example is in post response #11