2011-09-05 49 views
1

在Haskell即時通訊新,即時通訊做我的任務,其對銀行系統,我是說我要註冊新帳號程序錯誤此錯誤:Prelude.read:沒有解析程序錯誤:Prelude.read:沒有解析

的代碼如下:

createAcc :: IO() 
createAcc = do 
     new <- readFile "db.txt"       --Database named db-- 
     let new1 = length (func new) 
     putStrLn " Enter your Name : "  --write your name-- 
     name <- getLine 
     putStrLn " Enter Balance :"   --enter minimum balance to deposit-- 
     bal <- getLine 
     let bal1 = read bal :: Int       
     store <- readFile "db.txt"     --entries comes in database-- 
     let store1 = func store 
     let store2 = store1 ++ [(new1+1,name,bal1)] 
     writeFile "db.txt" (show store2) 
func :: String -> [(Int,String,Int)] 
func x = read x:: [(Int,String,Int)] 
+2

你不能在許多懶惰的'readFile'和'writeFile'調用中混合使用同一個文件。你需要明確地管理你的手柄,確保它們根據需要被打開和關閉。 –

回答

1

有可能沒有在db.txt,因此讀取失敗。嘗試用文本「[]」初始化文件。

此外,在您的方法中還有很多事項需要注意......懶惰的IO不適合編寫可靠的程序。您可以在網上找到更多信息,但實際上只有在您實際訪問文件內容時纔會發生讀取,例如,與func。至少,您應該使用deepseq強制讀取發生在您期望的位置。