2013-07-02 20 views
4

一個字符串變量我有這個功能的工作,其打印出的offsetmap值:獲取導致進入OCaml中

let pretty_offsetmap_original lv fmt offsetmap = 
    begin match offsetmap with 
    | None -> Format.fprintf fmt "<BOTTOM>" 
    | Some off -> 
    let typ = Some (typeOfLval lv) 
    in 
    Format.fprintf fmt "%a%a" 
     pretty_lval_or_absolute lv 
     (Cvalue.V_Offsetmap.pretty_typ typ) off 
end 

現在我想獲得的價值在一個字符串變量轉化爲我的目的。我用Printf.sprintf替換了Format.fprintf fmt,但它不起作用。該編譯錯誤:

Error: This expression has type 
     Format.formatter -> Cvalue.V_Offsetmap.t -> unit 
    but an expression was expected of type unit -> 'a -> string 

回答

4

不幸的是,你是對的:Format.sprintf沒有好的類型。在Frama-C中,函數Pretty_utils.sfprintf將完全符合您的需求。您可能還想看看Pretty_utils.to_string

+0

它的工作原理,非常感謝鮑里斯 – user2544482

3

好像意味着你需要Format.sprintfPrintf.sprintf更換Format.fprintf

+2

不幸的是,這並不適用'Format.sprintf'類型。 'Format.sprintf'%a「'具有類型'(unit - >'_a - > string) - >'_a - > string'而不是'(Format.formatter - >'_a - > string) - >'_a - > string' – byako