简体   繁体   English

如果在读取之前它不存在,则haskell写入文件

[英]haskell write to file it if it does not exists before reading

I am learning haskell and am stumbled upon the parse error on input contents' compilation error. 我正在学习haskell并且偶然发现了parse error on input contents'编译错误的parse error on input contents'错误。

What i want to do : 我想做的事 :

I store my previous session state state in a file. 我将以前的会话状态存储在一个文件中。 I read this file before starting the program. 我在启动程序之前读了这个文件。 However, during the first run of program, file may not exist. 但是,在第一次运行程序期间,文件可能不存在。 In this case, i want to first create file first with default value and then proceed ahead. 在这种情况下,我想首先使用默认值创建文件,然后继续。

main :: IO()
main = do
    -- Take input
   let fileName = "ashish-temp.txt"

   let dummyBoard = take 5 $ repeat "-----"

   fileExist <- doesFileExist fileName

   if False == fileExist
   then writeFile fileName $ unlines dummyBoard

   -- getting an error on this line
   contents <- readFile fileName

   -- do processing () :)

   --  i want the value in contents
   putStrLn "Done"

Also, i think that rather than writing dummyBoard to the file i can just initialize the contents with dummyBoard . 另外,我认为不是将dummyBoard写入文件,而是可以使用dummyBoard初始化内容。 But i also failed in doing it. 但我也做不到。 And i guess the way should be the same for both. 我猜两种方式应该是一样的。

Please help. 请帮忙。 Thanks. 谢谢。

Edit Solution: 编辑方案:

else is required for every if in haskell. 在haskell中每个if else需要else

Also another problem that you would face after this problem is : *** Exception: ashish-temp.txt: openFile: resource busy (file is locked) 此问题后您将面临的另一个问题是: *** Exception: ashish-temp.txt: openFile: resource busy (file is locked)

use import qualified System.IO.Strict as S and S.redFile for reading file. 使用import qualified System.IO.Strict as SS.redFile来读取文件。

There are some problem with your codebase: 您的代码库存在一些问题:

  • You are missing the else part of your if expression. 你缺少if表达式的else部分。 In Haskell, since if is an expression , it would need the else part as opposed to other languages where the if-else are statements and the else part is not mandatory. 在Haskell中,因为if是一个表达式,所以它需要else部分而不是其他语言,其中if-else是语句而else部分不是必需的。
  • What exactly is dim ? 究竟是什么dim You have to define it. 你必须定义它。

A working program which shows similar in concept of what you want to do will look like this: 一个工作程序显示类似于你想要做的概念将如下所示:

main :: IO()
main = do

   let fileName = "somefile.txt"

   fileExist <- doesFileExist fileName

   if not fileExist
   then writeFile fileName "something"
   else return ()

   contents <- readFile fileName
   -- do stuff with contents here

   putStrLn "Done"

You had a second part of the question which hasn't been answered yet. 你有问题的第二部分尚未得到答复。

Also, i think that rather than writing dummyBoard to the file i can just initialize the contents with dummyBoard . 另外,我认为不是将dummyBoard写入文件,而是可以使用dummyBoard初始化内容。 But i also failed in doing it. 但我也做不到。 And i guess the way should be the same for both. 我猜两种方式应该是一样的。

Indeed you can, as follows: 的确,你可以如下:

contents <- if fileExist
    then readFile fileName
    else return $ unlines dummyBoard

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM