我在XP中的dos命令行使用ghci 6.10.4,在使用haskell-mode-2.4的emacs中也使用ghci 6.10.4
当运行在stdin上运行的程序时,有没有办法可以将文件重定向为stdin?例如,如果我有一个名为main的函数从stdin读取,我不能这样做:
*Main> main < words.txt
Run Code Online (Sandbox Code Playgroud)
还有另外一种方法吗?
另外我希望能够在ghci窗口中键入stdin,这似乎有效,但是EOF键是什么?我认为这是Ctrl-D但是不起作用.
如果您重新main打开文件本身,这将更容易.
import System.Environment
import System.IO
main :: IO ()
main = do
args <- getArgs
case args of
[] -> doStuff stdin
file:_ ->
withFile file ReadMode doStuff
doStuff :: Handle -> IO ()
doStuff = …
Run Code Online (Sandbox Code Playgroud)
*Main> System.Environment.withArgs ["main.txt"] main
在GHCi中,不要在stdin上给出EOF.如果这样做,所有进一步尝试从stdin读取都将失败:
Prelude> getLine *** Exception: <stdin>: hGetLine: illegal operation (handle is closed) Prelude> getContents *** Exception: <stdin>: hGetContents: illegal operation (handle is closed)