Search code examples
prologswi-prologgnu-prologiso-prolog

How truncate file length in Prolog?


How do I truncate the file length in Prolog?

I only find a set_stream_position/2 predicate in the ISO standard. But I don't find a set_stream_length/2 predicate in the major Prolog systems.

Similarly there is a stream property position/1, but I don't see nowhere a length/1 stream property. The latter would help in using set_stream_length/2.

What would be the workaround?

Bye


Solution

  • I think I got it !

    see this page...

    edit after @false comment, here a sketch of encapsulating code:

    set_file_size(Path, Size) :-
        setup_call_cleanup(
            open(Path, update, S),
            (   stream_property(S, reposition(true)),
                % stream_property(S, position(Q)),
                % set_stream_position(S, Q),
                seek(S, Size, bof, Size),
                set_end_of_stream(S)
            ),
            close(S)).
    

    This works, but relies on seek/4 builtin. I can't fully determine the status of such call WRT ISO compliance. It's listed under ISO IO, but untagged as compliant...

    Those 2 commented lines served to me to inspect the opaque term position/1. There is a stream_position_data to inquiry the values.