Page 1 of 1

Writing values to specific line of text file

Posted: Fri Aug 24, 2018 8:51 am
by Summertime01
Hi all,

I‘m looking for a way to write data into a text file. My app needs to have several fields for entering values. These values shall be written into a text file (sometimes all at the same time, but in rare cases also individually). Writing to a text file is not the problem, but I would like each field to be written into a specific line of the text file because on another occasion the data shall be read from that specific line. What‘s the „best“ way of doing this?

Thanks a lot for your help!

Re: Writing values to specific line of text file

Posted: Fri Aug 24, 2018 9:34 am
by AndyP
I do that a lot and find the easiest method is to

Read the txt file into a hidden field
then replace the line of the txt in the field
then save back.

This method has the advantage that for testing you can unhide the field and check whats going on.

Re: Writing values to specific line of text file

Posted: Fri Aug 24, 2018 9:37 am
by Klaus
Hi Summertime01,

welcome to the forum!

Since this is Livecode, you can access the TXT file (even its content like LINE, WORD etc.) directly with:
...
put fld "user field 666" into line 10 of url("file:" & path_to_your_txt_file)
...
Cool, isn't it? :D


Best

Klaus

Re: Writing values to specific line of text file

Posted: Fri Aug 24, 2018 9:42 am
by Summertime01
Thank you! :-)

Klaus, I actually tried what you suggested yesterday and it didn't work. Must have made a different mistake there. Will check the script. Thanks. :-)

Re: Writing values to specific line of text file

Posted: Fri Aug 24, 2018 9:48 am
by Klaus
You're welcome!

If you still have problem, please post the script(s) here and we will see what we can find.

Re: Writing values to specific line of text file

Posted: Fri Aug 24, 2018 11:05 am
by Summertime01
Thank you! :-)

It works now. Had a file path issue in my script. Umpfm!

Re: Writing values to specific line of text file

Posted: Fri Aug 24, 2018 10:09 pm
by dunbarx
Look more deeply into the "Chunk" stuff of LC.

Code: Select all

on mouseup
   get  "Hello Wrld"
   put "o" after char 1 of word 2 of it
   answer it
    get  "Hello WXrld"
   put "o" into char 2 of word 2 of  it
   answer it
end mouseup
Craig Newman