lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


On 2002/11/23 13:39:55, Markus Huber <pulse@evolution.org> wrote:

FYI, I did a test on WinNT4.

> The problem seems to occure only with Linux (the problem doesn't occure
> with FreeBSD / Mac OS X). The first example works fine also with Linux:
> 
>    fh,err=io.open("./Dummy","w+")
>    if err then print(err) end
>    
>    fh:write("A")
>    fh:write("B")
>    fh:write("C")
> 
>    fh:close()
> 
> Show File: --> ABC as expected

OK.

> But if I add a simple read() the return of read and the resulting file
> and are wrong.
> 
>    fh,err=io.open("./Dummy","w+")
>    if err then print(err) end
>    
>    fh:write("A")
>    fh:write("B")
>    fh:write("C")
>    
>    print(fh:read("*a")) --> this example returns an empty string
>    
>    fh:close()
> 
> Show File: --> <0 bytes> but should be also ABC

It becomes strange...
The print outputs the source of this code, and 0.
Dummy shows ABC, the source, and some seemingly random binary data.

Now, both Microsoft and other sources states that when opening a file for 
update (read and write), "output must not be directly followed by input 
without an intervening call to fflush() or to a file positioning function 
(fseek() , fsetpos() or rewind()), and input must not be directly followed 
by output without an intervening call to a file positioning function, 
unless the input operation encounters end-of-file." 
<http://www.opengroup.org/onlinepubs/007908799/xsh/fopen.html> 

The behavior is probably "undefined" otherwise, and that's what we get.

>    fh,err=io.open("./Dummy","w+")
>    if err then print(err) end
> 
>    fh:write("Abc")
>    fh:write("Def")
> 
>    fh:seek("set",1)
> 
>    fh:write("Ghi")
> 
>    print(fh:read("*a"))
> 
>    fh:close()
> 
> Show File: --> AbcDef but should be AbcGhi

The print outputs Def/
The file shows AGhiDef/[NUL]e (where [NUL] is the null byte, the source of 
the code, and binary data.

Same here, you follow a write directly by a read, without flush nor seek 
in-between.

The problem is in the underlying C library, Lua can do nothing for this.

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