I have an error ‘openFile – permission denied’ when I try write something to file:
saveFile content path = do
writeFile path (show content)
why ?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
From your questions, it appears that you’re using
readFileandwriteFilefromSystem.IOon the same file.Remember that
System.IO.readFileis lazy, meaning that the file handle is opened, initially, and then as your program requires data, pieces will be read. Only once all data has been read, or all references to the file dropped, will the file handle be closed. Until then the resource is locked.You need to ensure the file is read fully before trying to write to it, or otherwise closed the file in some way. A simple way to achieve this is to use strict IO. E.g,:
Here, we ask for the length of the string, before returning the result. This forces the entire contents to be read.
This pattern is captured in the strict package.