File position unicode_le and utf8 and octet

I use file position data to get the file position when reading through a file, later on when the file is closed , and when new data needs to be read again i use seek to get to a certain position.

I found out that always when im in unicode_le - mode i must divide the position by 2 and then use seek function with that value, when in utf8 -mode i can use the position directly.

Is this intended and will it remain constant in future versions? If so i will not/never have to update the code.

Is the division with 2 not needed in octet -mode and ascii -mode?

You can only meaningfully use seek/4 on binary files. For text stream use stream_property/2 with the position property and restore using set_stream_position/2. That also restores the stream’s notion of line count, line position, etc. This is the only route that is supported by the ISO standard.

I’d expect multiply, no? How exactly do you get the current position? SWI-Prolog supports several older and more recent solutions for that …

dear Jan, thankyou for the information.
, on the server i use an older versie of swi prolog, the code I use works,
as you write it would be better to use set_stream_position i will try that

I use this code to obtain the position data :

stream_property(Sea, position(Kpos)),
stream_position_data(byte_count, Kpos, ChaCount),

then i use this to try to get back to the same position
Px2 is Pox / 2, seek(Sea, Px2, bof, _), !.

I see I was a bit off in my comments. The seek/4 docs specify how the unit size depends on the encoding for text files. So your method should be safe. The official ISO route is supported for a very long time though and both ensures full restore of the stream context and portability.