[Haskell-beginners] File I/O: reading from, writing to, same file in one function

Francesco Ariis fa-ml at ariis.it
Thu Feb 5 20:26:39 UTC 2015


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


More information about the Beginners mailing list