Read a file

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Post Reply
problème
Posts: 77
Joined: Fri Oct 23, 2015 12:03 am

Read a file

Post by problème » Wed Nov 25, 2015 3:48 pm

Hello,
I have a directory "dir", in my directory i have two file, myApp.livecode and langue.txt
I want read langue.txt so i do this but it's don't work , i have nothing in my variable texte

Code: Select all

on mouseUp
   open file specialFolderPath ("dir/langue.txt") for read
   read from file ("dir/langue.txt") until EOF
   put it into texte
   close file ("dir/langue.txt") 
   put texte
end mouseUp

Klaus
Posts: 14199
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Read a file

Post by Klaus » Wed Nov 25, 2015 3:52 pm

Bonjopur problème,

you did read the entry about "specialfolderpath" codes in the dictionary, didn't you? 8)
...
put specialfolderpath("resources") & "/langue.txt" into tFile
put url("file:" & tFile) into teste
put texte
...
For smallish files you can use the URL syntax and read the complete file "en bloc"!
Same for writing to file:
...
put fld "your field to save here" into url("file:" & tFile)
...


Best

Klaus

problème
Posts: 77
Joined: Fri Oct 23, 2015 12:03 am

Re: Read a file

Post by problème » Wed Nov 25, 2015 6:23 pm

I try to write in the file but i get a error at the line 2 "open file tFile for text " : button "Button": compilation error at line 7 (open: no mode specified), char 19

Code: Select all

on mouseUp
   put specialfolderpath("resources") & "/langue.txt" into tFile
   open file tFile for text 
   write "égegrfg" to file "langue.txt" 
   write "FIN" to file tFile at end 
   close file tFile
end mouseUp

quailcreek
Posts: 746
Joined: Sun Feb 04, 2007 11:01 pm

Re: Read a file

Post by quailcreek » Wed Nov 25, 2015 7:01 pm

Hi problème,
What platform is your app written for MAC, Win, iOS? I believe you said that the txt file you want to read is in the same directory are you app, right?
Tom
MacBook Pro OS Mojave 10.14

problème
Posts: 77
Joined: Fri Oct 23, 2015 12:03 am

Re: Read a file

Post by problème » Wed Nov 25, 2015 7:07 pm

Hello,
I want write my app for mac, windows and linux. And yes the text file and my app are the same directory

I found a solution for write in the file:

Code: Select all

on mouseUp
    put specialfolderpath("resources") & "/langue.txt" into tFile
    open file tFile for write
    write "rhghrghrh" to file tFile
    close file ("file:" & tFile)
end mouseUp
thanks for the help

Klaus
Posts: 14199
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Read a file

Post by Klaus » Wed Nov 25, 2015 7:24 pm

This line is not valid syntax: " open file tFile for text"!
"... for write" is the correct syntax, as you have found out.

Post Reply