Mercury Bugs - mercury
View Issue Details
0000375mercuryBugpublic2015-01-19 14:522015-01-21 01:07
Reporterlpimmes 
Assigned To 
PriorityurgentSeveritymajorReproducibilityalways
StatusnewResolutionopen 
PlatformOSx 10.9OSOS Version
Product Version 
Target VersionFixed in Version 
Summary0000375: io.seek_binary_output(Stream, set, 32, Out3, Out4), % rec1 NOT working
DescriptionSeeking to an offset, the second record, in binTest file and then writing new results only writes a single record.
Initially three records present; now only a single record remains.
Any ideas? Thanks.
This invites the question: overwrite record at position, versus insert new record at seek position.
Steps To ReproduceChange true/false switches in main/2 to achieve results for test:
1 write binTest, true, false
2 false, true read binTest at offset, record 2, OK
3 false, false write to binTest at offset, record 2; fails.

                                % ioBinFile 522>which mmc
                % /usr/local/mercury-14.01/bin/mmc

                % ioBinFile 511>./ioBinFile
                % Opened 'binTest' successfully.
                % Offsets rec0: 0, rec1: 32, rec2: 62.
                % ioBinFile 513>cat binTest
                % rec("The man ran home.", 3092).
                % rec("The girl saw me.", 312).
                % rec("Each person saw you.", 532).

                % set to false, recompile main/2
                % ioBinFile 517>./ioBinFile
                % Opened 'binTest' successfully.
                % Offset after seek: 32.
                % Read record: rec('The girl saw me.', 312)

                % Not working, just record output; first and third one removed.
                % false, false; default last term in if / then / else
                % ioBinFile 521>./ioBinFile
                % Opened 'binTest' successfully for seek writing.
                % Stream name: binTest; seek to position: 32.
                % ioBinFile 521>cat binTest
                % rec("Dr. Smith", 3).
TagsNo tags attached.
Attached Files? ioBinFile.m (4,993) 2015-01-19 14:52
https://bugs.mercurylang.org/file_download.php?file_id=234&type=bug

Notes
(0000805)
juliensf   
2015-01-20 12:33   
The call to io.open_binary_output/4 in the predicate writeSeek/3 will truncate the file.
You probably meant to call io.open_binary_append/4.
(0000806)
lpimmes   
2015-01-21 01:07   
Yes, I agree, and testing shows that o.open_binary_append/4 in writeSeek/3 will (not surprisingly)
append new text to end of existing text. Code and example shown below.
But, how do overwrite/insert at a particular seek position in this file?
Reading from a position after seek works fine.
Thanks.

:- pred writeSeek(string::in, io::di, io::uo) is det.
writeSeek(Fname, In, Out) :-
    io.open_binary_append(Fname, Result, In, Out2), % file may / may not exist

    (if Result = ok(Stream)
                % Offsets rec0: 0, rec1: 32, rec2: 62.
    then
    io.format("Opened '%s' successfully for seek writing.\n", [s(Fname) ], Out2, Out3),
     io.seek_binary_output(Stream, set, 32, Out3, Out4), % rec1
     io.binary_output_stream_offset(Stream, Offset, Out4, Out5),
     io.binary_output_stream_name(Stream, Name, Out5, Out6),
     io.format("Stream name: %s; seek to position: %i.\n",
           [s(Name), i(Offset) ], Out6, Out7), % should be 32, OK
     Rec0 = rec("Dr. Smith", 3),
     io.write_binary(Stream, Rec0, Out7, Out8),
     io.close_binary_output(Stream, Out8, Out)
    
    else if
    Result = io.error(Code)
    
    then
    io.format("Failed to open %s because of: %s.\n",
          [ s(Fname), s(io.error_message(Code)) ], Out2, Out)
    
    else
    io.format( "Fail to unify io.error(Code).\n", [], Out2, Out)
    ).

-----
ioBinFile 508>./ioBinFile
Opened 'binTest' successfully for seek writing.
Stream name: binTest; seek to position: 32.

ioBinFile 509>cat binTest
rec("The man ran home.", 3092).
rec("The girl saw me.", 312).
rec("Each person saw you.", 532).
rec("Dr. Smith", 3).

Issue History
2015-01-19 14:52lpimmesNew Issue
2015-01-19 14:52lpimmesFile Added: ioBinFile.m
2015-01-20 12:33juliensfNote Added: 0000805
2015-01-21 01:07lpimmesNote Added: 0000806