4

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

  • Do you want `set_stream_length/2`? Why should this operation be of relevance for streams in general? – false Dec 26 '12 at 17:36
  • There are several issues here: `length` meaning number of bytes (C/Java) vs. characters; the validity of this operation (when it cuts part of a multi-octet character); and the fact that this only makes sense for files. – false Dec 26 '12 at 18:16
  • `set_stream_position/2` is well defined only, if you use a position obtained by `stream_property/2`. You cannot make any assumptions about it, except that it is a ground term and that it uniquely identifies a certain position. – false Dec 26 '12 at 18:25

1 Answers1

1

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.

CapelliC
  • 59,646
  • 5
  • 47
  • 90