Version française
Home     About     Download     Resources     Contact us    

This site is updated infrequently. For up-to-date information, please visit the new OCaml website at

Browse thread
[Caml-list] undo_input_line
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2002-08-09 (12:45)
From: Xavier Leroy <xavier.leroy@i...>
Subject: Re: [Caml-list] undo_input_line
> Does `let s = input_line ic` always increment `pos_in ic` by 
> `String.length s + 1` ?

Only if "ic" is in binary mode.  In text mode, conversions may happen
during reading (e.g. under Windows, removal of the final \r) that
invalidate this claim.

> So, if I define a function
> let undo_input_line ic s = 
> seek_in ic (pos_in ic - String.length s - 1);;
> will it always work as expected, or will this depend on I/O mode (binary vs 
> text) and file format (DOS vs UNIX)?

To "undo" an input, I'd recommend doing "let p = pos_in ic" before the read,
performing the read, and doing "seek_in ic p" to undo the read.
This will be a lot more robust.

- Xavier Leroy
To unsubscribe, mail Archives:
Bug reports: FAQ:
Beginner's list: