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

ARC Pro will give you immediate updates and new features needed to unleash your robot's potential!

#9  

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

PRO
Synthiam
#10  

Like this...

Writing...


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

filedelete($filename)

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

Saving...


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

filereadreset($filename)

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

filereadclose($filename)

#12  

Is there a way to specify a line to over write?

PRO
Synthiam
#13  

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

#14  

@DJ What would be the script structure to read and write and close the same file multiple times from within one script?

#15  

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.

#16  

@Justin, thanks for the suggestion, however I had hoped that @DJ could answer for me.