2011-09-05 52 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强制读取发生在您期望的位置。