逐行读取文件 [英] Read a file line by line

查看:92
本文介绍了逐行读取文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试逐行读取文件,但当我到达EOF时我不知道如何停止。



我有这样的代码:

  readWholeFile = do inputFile < -  openFileexample.txtReadMode 
readALine inputFile

readALine x = do y< - hGetLine x
print y
readALine x





编辑:确切地说,它总是终止引发异常。

错误消息是:

  ***例外:example.txt:hGetLine:文件结尾


解决方案

您正在寻找的是,hIsEOF



查看 http://book.realworldhaskell.org/read/io.html


I'm trying to read a file line by line, but I don't know how to stop when I reach the EOF.

I have this code:

readWholeFile = do inputFile <- openFile "example.txt" ReadMode
                readALine inputFile

readALine x = do y <- hGetLine x
                 print y
                 readALine x

and it obviously always terminata raising an exception.

How can I solve?

Edit: exact error message is:

*** Exception: example.txt: hGetLine: end of file

解决方案

What you are looking for is, hIsEOF

Check out http://book.realworldhaskell.org/read/io.html

这篇关于逐行读取文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆