[erlang-questions] Bug or feature or my ignorance?

Joe Armstrong erlang@REDACTED
Sun Nov 1 18:14:49 CET 2015


Is this a bug or feature or just my ignorance?

I have a file which I open for write and then I do a
random access pwrite at byte 11 of the file
but bytes 0..10 get overwritten.

Here's the example:



test() ->
    %% make a file with 10 bytes
    {ok, S} = file:open("tmp",[raw,write,binary]),
    file:pwrite(S,0,<<0,1,2,3,4,5,6,7,8,9>>),
    file:close(S),
    %% reopen file and write from byte 11 onwards
    {ok, S1} = file:open("tmp",[raw,write,binary]),
    file:pwrite(S1,10,<<10,11,12,13,14,15>>),
    file:close(S1),
    file:read_file("tmp").


> bug:test().
{ok,<<0,0,0,0,0,0,0,0,0,0,10,11,12,13,14,15>>}

If I open the file in mode [raw,write,binary,append]
Then file beginning is not overwritten.

So opening an existing file for writing and writing to the end of file
messes up the portion of the file that I have not written to.

Is this the expected behaviour?

/Joe



More information about the erlang-questions mailing list