equivalent of seek'ing in a file?

Bengt Kleberg <>
Fri Aug 30 10:20:14 CEST 2002


> To: 
> Date: Fri, 30 Aug 2002 09:50:59 +0200
> From: Raimo Niskanen <>

thank you for answering.

> So, what is the result of running your escript?
> 
File "/tmp/afile"
line o1
line o2
line o3

line o1
line o2

File "/tmp/afile"
line o1
line o2
line o3
line w1

line o2

File "/tmp/afile"
line w1
line o2
line o3
line w2


> Anyway, try file:open(File, [read, append]), it might work as you
> expected.

yes it does. ie, all writes are done at the end of the file (as
promised in the documentation).


> I should expect that the [read, write] flags will start writing from the
> beginning of the file, see the erlang man page for the module 'file'
> function 'open', then will io:get_line/2 read from the position where
> the write stopped until newline, and then would a subsequent write
> continue where io:get_line/2 stopped, ...

those where my expectations, too. it does not appear to work that way,
though.


> It might be so that the write and read positions are not the same, but I
> think they are. Check the man pages for the Unix system calls 'read' and
> 'write'. The 'file' module tries to be Posix (Unix).

does the io module have any stated direction of posix compatibility, too?


> It might also be so that io:get_line/2 reads ahead and keeps data in a
> buffer so that file:write/2 writes from the position where io:get_line/2
> last read data with file:read/2. In general try to not mix read/write
> calls from different abstraction levels, in this case 'file' vs 'io'.
> Try using io:read/2 instead that should know of buffered data in the io
> server process and compensate for it. This should give a more consistent
> behaviour (unless there is a bug).

i am using the io module for all read/write operations. not the file module.

my file contents happens to be line oriented (long exposure to unix).
so io:get_line and io:fwrite (the latter with a ~n at the end of the
format string) seemed a natural way of doing things. i could even try
to live with the lack of cursor positioning (fseek()). but the (pseudo)
randomness of read/write operations made me want to find out what the
proper, documented, behaviour is.


bengt




More information about the erlang-questions mailing list