Note that there are some explanatory texts on larger screens.

plurals
  1. POErlang file truncation
    primarykey
    data
    text
    <p>I'm trying to write a basic Erlang program that reads in a file in one process and writes the file in another process. I'm finding that sometimes the output file gets truncated.</p> <p>For instance I wrote eunit test. If I run the single test <strong>drive_test:write_file_test()</strong> the output is correctly written. But running <strong>drive_test:test()</strong> truncates the output file in a different place each time. </p> <p>Do I need to do something special to make sure that the process finishes writing before it closes?</p> <p>drive.erl:</p> <pre><code>-module(drive). -include("library.hrl"). -export([init/1]). init(Drive) -&gt; loop(Drive). loop(Drive) -&gt; receive {write, Data} -&gt; write(Drive,Data), loop(Drive); close -&gt; close(Drive) end. write(Drive,Data) -&gt; %io:format("~p", [Data]), Handler = Drive#drive.volume, file:write(Handler, [Data]). close(Drive) -&gt; Handler = Drive#drive.volume, file:close(Handler), io:format("closed ~w~n", [Drive]). </code></pre> <p>drive_test.erl</p> <pre><code>-module(drive_test). -include_lib("eunit/include/eunit.hrl"). -include("library.hrl"). startupShutdown_test() -&gt; DrivePid = spawn(drive,init,[#drive{number=1}]), DrivePid ! close. write_basic_test() -&gt; {ok, F} =file:open("test/library/eunit.txt", write), DrivePid = spawn(drive,init,[#drive{number=1,volume=F}]), DrivePid ! {write, "Some Data"}, DrivePid ! close. write_file_test() -&gt; {ok, Fin} = file:open("cathedral.pdf", read), {ok, Fout} =file:open("test/library/eunit2.txt", write), DrivePid = spawn(drive,init,[#drive{number=1,volume=Fout}]), write_file( Fin, DrivePid), DrivePid ! close. write_file(F, DrivePid ) -&gt; Rd = file:read(F, 256), case Rd of {ok, Data} -&gt; DrivePid ! {write, Data}, write_file(F, DrivePid ); eof -&gt; file:close(F); _ -&gt; ?_assert(false) end. </code></pre> <p>truncated file:</p> <pre><code>$ ls -l cathedral.pdf test/library/eunit2.txt -rwx------+ 1 218879 Sep 16 22:21 cathedral.pdf -rwxr-xr-x 1 60928 Dec 17 09:31 test/library/eunit2.txt </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload