0

I have a Haskell program that shows a prompt and then accepts input from the command line. I'm doing this as:

main = do putStr "Please enter program source file name: "
          programFileName <- getLine
          programFileHandle <- openFile programFileName ReadMode
          program <- hGetContents programFileHandle
          putStr "Please enter initial file configuration file name: "
          initConfigFileName <- getLine
          initConfigFileHandle <- openFile initConfigFileName ReadMode
          initConfigStr <- hGetContents initConfigFileHandle
          print (evaluateProgram (lines program) (readReg initConfigStr))

When I run it on the GHCi interpreter, the prompts show up fine and I am able to enter my inputs (and everything else works).

e.g. *Main> main Please enter program source file name: sum.urm Please enter initial file configuration file name: sum.conf 9

When I compile it though (on Mac OS X or Windows), it produces an executable that does not show my prompts. It waits for the two input strings, and then once I have put in the valid filenames, it prints the prompts and the result.

e.g.

$ ./a.out 
sum.urm
sum.conf
Please enter program source file name: Please enter initial file configuration file name: 9

Any ideas why this is happening?

For the curious, I was implementing an Unlimited Register Machine in Haskell.

rdasxy
  • 1,641
  • 4
  • 16
  • 22

2 Answers2

5

The standard output stream, stdout, is line buffered by default. That means that it will only be written to the console every time you output a \n character, or finally when the program terminates. You can fix this by importing System.IO and doing hFlush stdout after every putStr that doesn't contain a \n at the end.

dflemstr
  • 25,947
  • 5
  • 70
  • 105
0

This is a buffering issue. Here are related questions, with several choices for solutions:

Why isn't my IO executed in order?

Execution order with (>>=) not what I expected

Community
  • 1
  • 1
amindfv
  • 8,438
  • 5
  • 36
  • 58