# File position unicode\_le and utf8 and octet

**URL:** <https://swi-prolog.discourse.group/t/file-position-unicode-le-and-utf8-and-octet/4036>\
**Category:** General\
**Created:** [June 7, 2021, 6:15am UTC](https://swi-prolog.discourse.group/t/file-position-unicode-le-and-utf8-and-octet/4036 "2021-06-07T06:15:05Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![drspro](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/drspro/32/3509_2.png) [@drspro](https://swi-prolog.discourse.group/u/drspro)\
**Post date:** [June 7, 2021, 6:15am UTC](https://swi-prolog.discourse.group/t/file-position-unicode-le-and-utf8-and-octet/4036/1 "2021-06-07T06:15:05Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![jan](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/jan/32/4_2.png) [@jan](https://swi-prolog.discourse.group/u/jan)\
**Post date:** [June 7, 2021, 6:42am UTC](https://swi-prolog.discourse.group/t/file-position-unicode-le-and-utf8-and-octet/4036/2 "2021-06-07T06:42:03Z")

</div>

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.

> [@drspro](#):
>
> I found out that always when im in unicode\_le - mode i must divide the position by 2

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

---

<div class="post-metadata">

**Author:** ![drspro](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/drspro/32/3509_2.png) [@drspro](https://swi-prolog.discourse.group/u/drspro)\
**Post date:** [June 7, 2021, 7:27am UTC](https://swi-prolog.discourse.group/t/file-position-unicode-le-and-utf8-and-octet/4036/3 "2021-06-07T07:27:16Z")

</div>

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, \_), !.

---

<div class="post-metadata">

**Author:** ![jan](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/jan/32/4_2.png) [@jan](https://swi-prolog.discourse.group/u/jan)\
**Post date:** [June 7, 2021, 7:59am UTC](https://swi-prolog.discourse.group/t/file-position-unicode-le-and-utf8-and-octet/4036/4 "2021-06-07T07:59:11Z")

</div>

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.
