haskell的getLine總在putStr之前

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
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章