Writing to a file.

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Writing to a file.

Gaurav Kumar
Hi all,
       I am still in the early stages of learning Lua.
I am using Lua 3.2. I have this problem, i can read from
a file , but cannot write to it.

-- et contains "happy"

k = openfile("et","r")
print(k)
m= read(k,"*l")
print(m)

 it works fine

but this does not ?

k = openfile("etc","w")
print(k)
m= write(k,"happy")
print(m)

though a new file is created it has nothing

and userdata:0 is returned from write.


Thanks
Gaurav


Reply | Threaded
Open this post in threaded view
|

Re: Writing to a file.

Philippe Lhoste
On mar. 15 oct. 2002 08:01:48, Gaurav Kumar <[hidden email]> 
wrote:

>        I am still in the early stages of learning Lua.
> I am using Lua 3.2. I have this problem, i can read from
> a file , but cannot write to it.
> 
> -- et contains "happy"
> 
> k = openfile("et","r")
> print(k)
> m= read(k,"*l")
> print(m)
> 
>  it works fine
> 
> but this does not ?
> 
> k = openfile("etc","w")
> print(k)
> m= write(k,"happy")
> print(m)
> 
> though a new file is created it has nothing
> 
> and userdata:0 is returned from write.

I fear I can't help you much, but I would raise several points:
- You should indicate the platform on which you are running Lua... Are you 
sure you have the rights to write a file where you want? AFAIK, your code 
will (try to) write the etc file where your script is. You may want to 
specify a path.
- Unless you have a very specific need for it (mandatory at your place?), 
you should probably drop Lua 3.2 to use Lua 4.01. Lua 5.0 is on its way, 
but still alpha as the time of writting. Lua 3.2 isn't bad, of course, but 
a bit outdated and slightly more primitive :-)

-- 
--=#=--=#=--=#=--=#=--=#=--=#=--=#=--=#=--=#=--
Philippe Lhoste (Paris -- France)
Professional programmer and amateur artist
http://jove.prohosting.com/~philho/



Reply | Threaded
Open this post in threaded view
|

Re: Writing to a file.

Basile STARYNKEVITCH
>>>>> "Philippe" == Philippe Lhoste <[hidden email]> writes:

    Philippe> On mar. 15 oct. 2002 08:01:48, Gaurav Kumar
    Philippe> <[hidden email]> wrote:

    >> I am still in the early stages of learning Lua.  I am using Lua
    >> 3.2. I have this problem, i can read from a file , but cannot
    >> write to it.
    >> 
    >> -- et contains "happy"
    >> 
    >> k = openfile("et","r") print(k) m= read(k,"*l") print(m)
    >> 
    >> it works fine
    >> 
    >> but this does not ?
    >> 
    >> k = openfile("etc","w") print(k) m= write(k,"happy") print(m)
    >> 
    >> though a new file is created it has nothing
    >> 

As others I suggest switching to Lua4 at least.

But when writing files (and this is true in many languages) you need
to either flush or close them to have something in the file system.



-- 

Basile STARYNKEVITCH         http://starynkevitch.net/Basile/ 
email: basile<at>starynkevitch<dot>net 
alias: basile<at>tunes<dot>org 
8, rue de la Faïencerie, 92340 Bourg La Reine, France

Reply | Threaded
Open this post in threaded view
|

Re: Writing to a file.

Philippe Lhoste
On mar. 15 oct. 2002 10:01:07, Basile STARYNKEVITCH 
<[hidden email]> wrote:

> As others I suggest switching to Lua4 at least.
> 
> But when writing files (and this is true in many languages) you need
> to either flush or close them to have something in the file system.

Ouch, silly me, I didn't notice this culprit and instead of 
copying/pasting the actual code, I rewrote it quickly, my way:

f = openfile("FooBar.txt", "w")
print(write(f, "Doh!"))
closefile(f)

which works, of course, and do print "userdata(0): 00000000".
In Lua 4.0, "Unless otherwise stated, all I/O functions return nil on 
failure and some value different from nil on success." and this "some 
value" is actually an empty userdata.
In Lua 5.0, we get "true"...

Welcome to the wonderful world of Lua, Gaurav, I hope you will enjoy your 
stay :-)

-- 
--=#=--=#=--=#=--=#=--=#=--=#=--=#=--=#=--=#=--
Philippe Lhoste (Paris -- France)
Professional programmer and amateur artist
http://jove.prohosting.com/~philho/