equivalent of seek'ing in a file?

Raimo Niskanen <>
Fri Aug 30 09:50:59 CEST 2002


So, what is the result of running your escript?

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

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, ...

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).

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).

/ Raimo Niskanen, Erlang/OTP, Ericsson AB



Bengt Kleberg wrote:
> 
> greetings,
> 
> can i position the 'cursor' when read/writing to a file?
> 
> it seems (test included below) that after file:open( File, [read, write] ),
> io:fwrite() will put characters in the beginning of the file. until i
> have made the first io:get_line(). then characters will start writing
> at the end of the file.
> 
> is there some rule/documentation describing this?
> 
> bengt
> 
>   ------------------------------------------------------------------------
> #! /usr/bin/env escript
> 
> -export([main/1]).
> 
> main( Args ) ->
>         true = code:add_patha( '/home/eleberg/private/erlang/src' ),
>         File = file( Args ),
>         fill_file( File ),
>         display( File ),
>         readwrite( File ),
>         display( File ),
>         fill_file( File ),
>         writeread( File ),
>         display( File ),
>         halt().
> 
> fill_file( File ) ->
>         {ok, Io} = file:open( File, [write] ),
>         io:fwrite( Io, "line o1~n", [] ),
>         io:fwrite( Io, "line o2~n", [] ),
>         io:fwrite( Io, "line o3~n", [] ),
>         file:close( Io ).
> 
> readwrite( File ) ->
>         {ok, Io} = file:open( File, [read, write] ),
>         io:fwrite( "~s", [io:get_line( Io, '' )] ),
>         io:fwrite( Io, "line w1~n", [] ),
>         io:fwrite( "~s", [io:get_line( Io, '' )] ),
>         file:close( Io ).
> 
> writeread( File ) ->
>         {ok, Io} = file:open( File, [read, write] ),
>         io:fwrite( Io, "line w1~n", [] ),
>         io:fwrite( "~s", [io:get_line( Io, '' )] ),
>         io:fwrite( Io, "line w2~n", [] ),
>         file:close( Io ).
> 
> 
> display( File ) ->
>         io:fwrite( "~nFile ~p~n", [File] ),
>         {ok, Bin} = file:read_file( File ),
>         io:format( "~s~n", [binary_to_list( Bin )] ).
> 
> file( [] ) ->
>         "/tmp/afile";
> file( [File] ) ->
>         File.



More information about the erlang-questions mailing list