[mercury-users] seek_binary

Fergus Henderson fjh at cs.mu.OZ.AU
Fri Nov 8 19:25:01 AEDT 2002


On 08-Nov-2002, Noel  Pinto <cool4life at rediffmail.com> wrote:
> I need some clarifications on the usage of io__seek_binary.
> 
> The pred is written as
> :- pred io__seek_binary(io__binary_stream, io__whence, int, 
> io__state, io__state).
> 
> io__whence is defined as
> :- type io__whence --->
>     set
> ;   cur
> ;   end

You omitted the comments:

        % io__whence denotes the base for a seek operation.
        %       set     - seek relative to the start of the file
        %       cur     - seek relative to the current position in the file
        %       end     - seek relative to the end of the file.

	:- pred io__seek_binary(io__binary_stream, io__whence, int, 
				io__state, io__state).
	:- mode io__seek_binary(in, in, in, di, uo) is det.
	%       Seek to an offset relative to Whence (documented above)
	%       on a specified binary stream. Attempting to seek on a
	%       pipe or tty results in implementation dependent behaviour.

> What I understand is that the syntax would be
> io__seek_binary(BinaryStream, cur, 10)

Yes, in a DCG, that would be the correct syntax.

> I have some queries regartding the usage of the syntax...
> 1) io__seek_binary(BinaryStream, set, 10)
> set is seeking to the start of the file, so plz do tell me if the 
> above syntax would work. How would the start of the file be 10??

It is seeking *relative to* the start of the file.
In this case, it would seek to 10 bytes after the start of the file.

> 2) io__seek_binary(BinaryStream, cur, 10)
> cur seeks relative in the file. What would happen if the current 
> position would be 15 in the above syntax??

If the current position was 15 bytes after the start of the file,
this command would seek to 25 bytes after the start of the file.

> 3) io__seek_binary(BinaryStream, end, 10)
> end seeks relative to the end of the file. What would happen if 
> the current position in the file is 20 and is not the last 
> line??

This would seek to 10 bytes after the current end of the file.

Note that for "end", you can use negative offsets to seek
to a position before the end of file.  Likewise, for "cur"
you can use negative offsets to move the current position
back torwards the start of the file.

-- 
Fergus Henderson <fjh at cs.mu.oz.au>  |  "I have always known that the pursuit
The University of Melbourne         |  of excellence is a lethal habit"
WWW: <http://www.cs.mu.oz.au/~fjh>  |     -- the last words of T. S. Garp.
--------------------------------------------------------------------------
mercury-users mailing list
post:  mercury-users at cs.mu.oz.au
administrative address: owner-mercury-users at cs.mu.oz.au
unsubscribe: Address: mercury-users-request at cs.mu.oz.au Message: unsubscribe
subscribe:   Address: mercury-users-request at cs.mu.oz.au Message: subscribe
--------------------------------------------------------------------------



More information about the users mailing list