Send Beginners mailing list submissions to
        [email protected]

To subscribe or unsubscribe via the World Wide Web, visit
        http://www.haskell.org/mailman/listinfo/beginners
or, via email, send a message with subject or body 'help' to
        [email protected]

You can reach the person managing the list at
        [email protected]

When replying, please edit your Subject line so it is more specific
than "Re: Contents of Beginners digest..."


Today's Topics:

   1.  File I/O: reading from, writing to,      same file in one
      function (Geoffrey Bays)
   2. Re:  File I/O: reading from, writing to, same file in one
      function (Francesco Ariis)
   3. Re:  Couldn't match expected type ?IO ()? with actual type
      [Integer] (Marcin Mrotek)
   4. Re:  Couldn't match expected type ?IO ()? with actual type
      [Integer] (Roelof Wobben)
   5. Re:  File I/O: reading from, writing to, same file in one
      function (Chadda? Fouch?)
   6. Re:  File I/O: reading from, writing to, same file in one
      function (Geoffrey Bays)


----------------------------------------------------------------------

Message: 1
Date: Thu, 5 Feb 2015 14:51:52 -0500
From: Geoffrey Bays <[email protected]>
To: [email protected]
Subject: [Haskell-beginners] File I/O: reading from, writing to,        same
        file in one function
Message-ID:
        <CAD8ukx5YQAPOwX87zipTVVEE4YeK=fnfus0g_qfzpgwy6+o...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"

Question:

As I work my way through Learn You as Haskell, I have been writing small
File IO programs.
Unlike the example in that book, where the author reads from one file,
writes altered data to a temp file and then deletes the original file and
renames the temp file, I have been trying to read from a given file, alter
the data and then overwrite the same file with the new data. Is this
possible?

In a main  do block I have tried:

contents <- readFile fileName

-- do stuff to contents--

writeFile fileName result

After using appendFile to add items to the file, when I call the function
to read and write I get this error:
openFile: resource busy, (file is locked)

I have also tried using the file handles like so:

    handle <- openFile fileName ReadWriteMode
    contents <- hGetContents handle
 -- do stuff to contents --

    hPutStr handle  results
   hClose handle

I have also tried opening with one handle to read, closing it, then using
another handle for writing,
but the error message is that the handle to the file is closed.

I am thinking that the the author of LYAH maybe has good reasons to use a
temp file, or is there another way that is not too difficult to grasp??

Many thanks,

Geoffrey
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://www.haskell.org/pipermail/beginners/attachments/20150205/e090e7d8/attachment-0001.html>

------------------------------

Message: 2
Date: Thu, 5 Feb 2015 21:26:39 +0100
From: Francesco Ariis <[email protected]>
To: [email protected]
Subject: Re: [Haskell-beginners] File I/O: reading from, writing to,
        same file in one function
Message-ID: <[email protected]>
Content-Type: text/plain; charset=us-ascii

On Thu, Feb 05, 2015 at 02:51:52PM -0500, Geoffrey Bays wrote:
> As I work my way through Learn You as Haskell, I have been writing small
> File IO programs.
> Unlike the example in that book, where the author reads from one file,
> writes altered data to a temp file and then deletes the original file and
> renames the temp file, I have been trying to read from a given file, alter
> the data and then overwrite the same file with the new data. Is this
> possible?

Try `withFile` [1], there is an example of its usage in chapter 9 of
LYAH [2].

[1] 
https://downloads.haskell.org/~ghc/6.12.2/docs/html/libraries/base-4.2.0.1/System-IO.html#v%3AwithFile
[2] http://learnyouahaskell.com/input-and-output#files-and-streams


------------------------------

Message: 3
Date: Thu, 5 Feb 2015 21:59:13 +0100
From: Marcin Mrotek <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] Couldn't match expected type ?IO ()?
        with actual type [Integer]
Message-ID:
        <cajcfpzkrv_cep8aj0w-f3jtz2t+tm_ccwlqoixcq0e-guxd...@mail.gmail.com>
Content-Type: text/plain; charset=UTF-8

@Norbert Melzer - Yeah, I just wanted to provide a solution without
shuffling Roelof's code too much, so he'd recognize how is it
different from his version. I did write that this is incomplete and
requires the list to be reversed later.

@Roelof Wobben - Maybe try a different one? There are several popular
choices other than LYAH:
* FP Complete Shool of Haskellhttps://www.fpcomplete.com/school
* Wikibooks: http://en.wikibooks.org/wiki/Haskell
* Real World Haskell: http://book.realworldhaskell.org (Warning: while
I remember learning from this book some time ago and it was fine,
every now and then I see people saying that it's outdated and some
examples don't compile)

Kind regards,
Marcin Mrotek


------------------------------

Message: 4
Date: Thu, 05 Feb 2015 22:12:31 +0100
From: Roelof Wobben <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] Couldn't match expected type ?IO ()?
        with actual type [Integer]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=windows-1252; format=flowed

Marcin Mrotek schreef op 5-2-2015 om 21:59:
> @Norbert Melzer - Yeah, I just wanted to provide a solution without
> shuffling Roelof's code too much, so he'd recognize how is it
> different from his version. I did write that this is incomplete and
> requires the list to be reversed later.
>
> @Roelof Wobben - Maybe try a different one? There are several popular
> choices other than LYAH:
> * FP Complete Shool of Haskellhttps://www.fpcomplete.com/school
> * Wikibooks: http://en.wikibooks.org/wiki/Haskell
> * Real World Haskell: http://book.realworldhaskell.org (Warning: while
> I remember learning from this book some time ago and it was fine,
> every now and then I see people saying that it's outdated and some
> examples don't compile)
>
> Kind regards,
> Marcin Mrotek
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
>

Thanks, Im followinh the CiS 194 course which can be found here : 
http://www.seas.upenn.edu/~cis194/spring13/index.html
and not the LYAH.

The last book is used on this course.

Roelof



------------------------------

Message: 5
Date: Thu, 5 Feb 2015 22:30:27 +0100
From: Chadda? Fouch? <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] File I/O: reading from, writing to,
        same file in one function
Message-ID:
        <canfjzryr_vxzslxv0iyyjmvvygaozdqckjgwze86bvq3cmn...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"

Hello,

On Thu, Feb 5, 2015 at 8:51 PM, Geoffrey Bays <[email protected]> wrote:

> Question:
>
> As I work my way through Learn You as Haskell, I have been writing small
> File IO programs.
> Unlike the example in that book, where the author reads from one file,
> writes altered data to a temp file and then deletes the original file and
> renames the temp file, I have been trying to read from a given file, alter
> the data and then overwrite the same file with the new data. Is this
> possible?
>

It is certainly possible, whether it is a good idea is another thing
entirely.


>
> In a main  do block I have tried:
>
> contents <- readFile fileName
>
> -- do stuff to contents--
>
> writeFile fileName result
>
>
This generally won't work because readFile is lazy, it only deliver a
promise of content without reading it immediately, to allow simple
streaming. To do that it has to hold on an opened handle to fileName, it
will only close it when the the EOF is reached in contents, so when
contents is completely evaluated, which depending on your code will
probably only happen when result is completely evaluated... Except it won't
be since writeFile would be the one doing this evaluation and writeFile
won't even start because it needs to open fileName in WriteMode and
fileName is still open in ReadMode...


> After using appendFile to add items to the file, when I call the function
> to read and write I get this error:
> openFile: resource busy, (file is locked)
>
> I have also tried using the file handles like so:
>
>     handle <- openFile fileName ReadWriteMode
>     contents <- hGetContents handle
>  -- do stuff to contents --
>
>     hPutStr handle  results
>    hClose handle
>

Interleaving lazy IO in the form of hGetContents and trying to write on the
same handle strike me as a really bad idea... Supposing it worked, where
exactly in the file did you think this would write ? ReadWriteMode is
generally a very bad idea (whatever the language) except if you're
manipulating a binary format with fixed length fields and, even then, it
requires good discipline.


>
> I have also tried opening with one handle to read, closing it, then using
> another handle for writing,
> but the error message is that the handle to the file is closed.
>


That should work, provided you do it properly but you probably still used
lazy IO so you're left with a promise of content but you closed the handle
that was supposed to provide this content... If you were to use strict IO
though that would mean you would have to get the entire content of your
file in memory which depending on the size of this file may be bad or even
catastrophic (hint : String is *extremely* wasteful, Text is better for
text and ByteString for binary formats).


>
> I am thinking that the the author of LYAH maybe has good reasons to use a
> temp file, or is there another way that is not too difficult to grasp??
>
> Right !
Reading from a file and writing to a temp file then closing everything and
renaming *is the proper way* to handle your workflow, it has only
advantages :

   - you can stream the content of your file while modifying it, using far
   less memory (RAM) in exchange for an insignificant bump in disk usage,
   - your modification is atomic (in a proper filesystem) : if your program
   is interrupted for whatever reason, you're not left with a corrupted file
   but the intact original and an incomplete temp file you can use for
   diagnostic

This is actually true whatever language you're using (and is the
recommended way by all experts) ! But in Haskell the other way is even
trickier to get right if you don't understand when you should use strict IO
(tip : getContents and readFile are both lazy IO and completely
inappropriate if controlling *when* the IO happens is important, like here).

So given that the other way is both harder to get right and generally a bad
idea anyway, I really would recommend to use the proper way.


-- 

Jeda?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://www.haskell.org/pipermail/beginners/attachments/20150205/a0470df0/attachment-0001.html>

------------------------------

Message: 6
Date: Thu, 5 Feb 2015 16:30:40 -0500
From: Geoffrey Bays <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] File I/O: reading from, writing to,
        same file in one function
Message-ID:
        <cad8ukx7fnjissc7lfkeez0txgkwdeajuewtheonxcg+mhqo...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"

Francesco:

Unfortunately, using withFile I get the same error as before with handles:
hPutStr: illegal operation (handle is closed).

Any other suggestions for reading and writing to the same file in the same
function?

Thanks

On Thu, Feb 5, 2015 at 3:26 PM, Francesco Ariis <[email protected]> wrote:

> On Thu, Feb 05, 2015 at 02:51:52PM -0500, Geoffrey Bays wrote:
> > As I work my way through Learn You as Haskell, I have been writing small
> > File IO programs.
> > Unlike the example in that book, where the author reads from one file,
> > writes altered data to a temp file and then deletes the original file and
> > renames the temp file, I have been trying to read from a given file,
> alter
> > the data and then overwrite the same file with the new data. Is this
> > possible?
>
> Try `withFile` [1], there is an example of its usage in chapter 9 of
> LYAH [2].
>
> [1]
> https://downloads.haskell.org/~ghc/6.12.2/docs/html/libraries/base-4.2.0.1/System-IO.html#v%3AwithFile
> [2] http://learnyouahaskell.com/input-and-output#files-and-streams
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://www.haskell.org/pipermail/beginners/attachments/20150205/108b6e2c/attachment.html>

------------------------------

Subject: Digest Footer

_______________________________________________
Beginners mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/beginners


------------------------------

End of Beginners Digest, Vol 80, Issue 6
****************************************

Reply via email to