2016-03-30 22 views
0

Temel olarak önbelleklerin bir listesi var ve bir önbellek 16 parametreli kullanıcı tanımlı bir tür. Benim kodumla, var olan bir dosyadan sadece varolan ilk önbelleği okuyor, dosyadaki tüm önbellekleri nasıl tekrar tekrar okuyorsunuz?Ocaml özyinelemeli bir dosya yükleme

önbellek zaten okunacak hazır:

let loadCache ci = 
    let code = input_line ci in 
    let name = input_line ci in 
    let state = input_line ci in 
    let owner = input_line ci in 
    let latitude = float_of_string (input_line ci) in 
    let longitude = float_of_string (input_line ci) in 
    let kind = input_line ci in 
    let size = input_line ci in 
    let difficulty = float_of_string (input_line ci) in 
    let terrain = float_of_string (input_line ci) in 
    let status = input_line ci in 
    let hiddenDate = input_line ci in 
    let nFounds = int_of_string (input_line ci) in 
    let nNotFounds = int_of_string (input_line ci) in 
    let nFavourites = int_of_string (input_line ci) in 
    let altitude = int_of_string (input_line ci) in { 
     code = code; name = name; state = state; owner = owner; 
     latitude = latitude; longitude = longitude; 
     kind = kind; size = size; difficulty = difficulty; terrain = terrain; 
     status = status; hiddenDate = hiddenDate; 
     nFounds = nFounds; nNotFounds = nNotFounds; nFavourites = nFavourites; 
     altitude = altitude 
    } 
;; 

Ve bu im (henüz recusive değil) okumak için yapmaya çalışıyor budur:

val load: string -> cache list 

let rec loadChannel ci = 
    try 
    loadCache ci 
    with End_of_file -> raise (Arg.Bad "loadChannel: no caches on this file to read")  
;; 

let load filename = (* post: result is never [] *) 
let ci = open_in filename in 
    let cl = loadChannel ci in 
     close_in ci; cl 
;; 

Ve durumunda

test etmek gerek

type cache = {   
    code: string;  
    name: string;  
    state: string;  
    owner: string;  
    longitude: float; 
    kind: string;  
    size: string;  
    difficulty: float; 
    terrain: float;  
    status: string;  
    hiddenDate: string; 
    nFounds: int;  
    nNotFounds: int;  
    nFavourites: int; 
    altitude: int  
} ;; 

son bir şey bu olup olmadığını test ve görmek için,: bunu oluşturmak zorunda kalmamak için buraya önbellek türü koyarak im Sadece tam yolunu yazın çalışan dosyanın nerede:

let rec loadChannel ci = 
    try 
    loadCache ci 
    with End_of_file -> raise (Arg.Bad "loadChannel: no caches on this file to read")  

yerine yazabilirsiniz:

let q = load "C: ...... file.txt" 
+2

Peki, her zaman cevap vermek için bir zevk ve geri bildirim yok ;-) – Lhooq

cevap

0

Eğer yazarken

let loadCache ci = 
    let code = try Some (input_line ci) with End_of_file -> None in 
    match code with 
    | None -> raise Exit 
    | Some code -> 
    try 
     let name = input_line ci in 
     let state = input_line ci in 
     let owner = input_line ci in 
     let latitude = float_of_string (input_line ci) in 
     let longitude = float_of_string (input_line ci) in 
     let kind = input_line ci in 
     let size = input_line ci in 
     let difficulty = float_of_string (input_line ci) in 
     let terrain = float_of_string (input_line ci) in 
     let status = input_line ci in 
     let hiddenDate = input_line ci in 
     let nFounds = int_of_string (input_line ci) in 
     let nNotFounds = int_of_string (input_line ci) in 
     let nFavourites = int_of_string (input_line ci) in 
     let altitude = int_of_string (input_line ci) in 
     { 
      code; name; state; owner; latitude; longitude; 
      kind; size; difficulty; terrain; status; hiddenDate; 
      nFounds; nNotFounds; nFavourites; altitude 
     } 
    with End_of_file -> raise (Arg.Bad "loadChannel: no caches on this file to read or not enough informations") 

let rec loadChannel ci acc = 
    try 
    let c = loadCache ci in 
    loadChannel ci (c::acc) 
    with Exit -> acc (* If we have an Exit, it means that the first read 
         was an End_of_file so it's not a mistake, just 
         a proper End_of_file. If this exception appears 
         after, it means that we were reading a cache and 
         couldn't finish the reading. *) 

let load filename = (* post: result is never [] *) 
    let ci = open_in filename in 
    let cl = loadChannel ci in 
    close_in ci; 
    match cl with 
     | [] -> raise BIGEXCEPTION 
     | _ -> cl 

Ve (önbellek güzel listesi var boş değil);)

Bu arada, bir kayıtta,

diyelim Saha adı 'a' ve 'b' olduğundan size

let a = 3 and b = 4 in {a = a; b = b} 

yazarsanız

type r = {a : int; b : int} 

, sen

let a = 3 and b = 4 in {a; b} 

yazabilir ve OCaml düzgün dolduracaktır. ;)