Version française
Home     About     Download     Resources     Contact us    
Browse thread
[Caml-list] scanf and %2c
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Alan Schmitt <alan.schmitt@p...>
Subject: [Caml-list] scanf and %2c
Hi,

As I needed to parse some string representing time (of the form hh:mm), 
I decided to use scanf. The correct code to do it is:
# let time_parse s =
  Scanf.sscanf s "%2s:%2s" (fun a b -> a,b) 
  ;;
val time_parse : string -> string * string = <fun>

but of course this is not what I tried first, thinking that I wanted 
a string of two chars:
# let time_parse s =
      Scanf.sscanf s "%2c:%2c" (fun a b -> a,b) 
      ;;
val time_parse : string -> char * char = <fun>

this leads to the following:

# time_parse "10:20" ;;
Exception: Scanf.Scan_failure "scanf: bad input at char number 2: 0".
# time_parse "1:2" ;;
- : char * char = ('1', '2')

So shouldn't there be a warning (or an error) when using a size field 
with chars ?

Alan

-- 
The hacker: someone who figured things out and made something cool happen.

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners