1.問題描述
在使用ghc編譯haskell程序後,getLine總是在putStr之前執行,儘管putStr放在了前面。這時只需要刷新IO,便可解決此問題。
2.解決方案
import System.IO
main = do
putStr "Input file: "
hFlush stdout --刷新標準輸出
ifile <- getLine
putStrLn ifile
putStr "Output file: "
hFlush stdout
ofile <- getLine
putStrLn ofile
3.編譯程序
$ ghc -o example example.hs
4.運行及其結果
$ ./example
Input file: test1
test1
Output file: test2
test2