equivalent of seek'ing in a file?

Bengt Kleberg eleberg@REDACTED
Thu Aug 29 13:42:01 CEST 2002


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
-------------- next part --------------
#! /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