Asked — Edited

Script Help - Reading From A File

Hi,

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.

Thanks.


ARC Pro

Upgrade to ARC Pro

Take control of your robot's destiny by subscribing to Synthiam ARC Pro, and watch it evolve into a versatile and responsive machine.

#1  

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
 

#2  

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
sleep(1000) 
$contents=FileReadAll("myfile.txt")  #read entire file info into $contents

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

FileReadClose("myfile.txt" ) #close the file when done with it
sleep(1000)
FileDelete("myfile.txt") # delete it if you have no more need for it

#3  

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)

#4  

@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.

#5  

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
#etc

So $contents1 through $contents4 variables store the data of the first 4 lines of data that was in the file respectively...

#6  

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?

PRO
Synthiam
#7  

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?

#8  

@DJ Silly Me.. Yes, you are correct!

:)