you can write this a bit more simply as: main = do (do do-the-major stuff here putStr "File created...") `catch` (\_ -> show the error) getLine -- look ma, no <- return () now, your problem is almost certainly with buffering. in the main do, put hSetBuffering stdout NoBuffering hSetBuffering stdin NoBuffering you'll need to import System.IO to get these. -- Hal Daume III | hdaume@isi.edu "Arrest this man, he talks in maths." | www.isi.edu/~hdaume -----Original Message----- From: haskell-cafe-admin@haskell.org [mailto:haskell-cafe-admin@haskell.org] On Behalf Of Alexandre Weffort Thenorio Sent: Tuesday, August 12, 2003 4:17 PM To: haskell@haskell.org Cc: haskell-cafe@haskell.org Subject: Help with Exceptions on I/O I have a program which creates textfiles out of other files. Since the program is runned from windows I output some text strings (Like "File created succefully") and I need to stop the program before it quits so that the user can read the line outputted to know what went on and then he can press ENTER to quit the program. I managed to do this fine if no error occurs but when a error occurs I am having problems. The code goes like that main :: IO() main =catch (do Do all the needed stuff here putStr "File created Successfully. Press RETURN to quit" dummy <- getLine --Halts the program so the user can read the above line) putStr "Exiting now..." --needed since I can't finish a do function with the "dummy<- getLine" line) (\_ -> do putStr "\nFile not found. Press RETURN (ENTER) to quit." dumb <- getLine putStr "\nExiting...") So when the program runs, if the input file is found the program writes successfull creation of file but if the file doesn't exist, after the user gives the input filename and press enter, the program creates a new line and Halts (Probably because of the getLine function) without writing out anything, then when the user press ENTER again it writes the line at the first putStr (File not...), then writes the next putStr line under it (Exiting...) and exits. I don't know why it doesn't wirte the first line, halts and then when user press enter it writes the second and quits. Can anybody help me as I am not very familiar with exception and catches. Another question I have is: Is there any other function rather than getLine that halts a program and continues when a user press any key (Instead of ENTER) and besides this is an ugly code since getLine wasn't made for that but I couldn't find anything else myself. Thank you in advance. Best Regards Alex
You don't really need to change the buffering mode. stdout is line buffered by default, so you just need to make sure a newline is printed after your message. putStrLn adds a newline after the string it prints, or you could use \n in the string literal. Try this: main = do --lots of code goes here, --with a catch handler if you want it putStrLn "Press ENTER to exit" -- with Ln getLine return () On Tue, 12 Aug 2003, Hal Daume wrote:
you can write this a bit more simply as:
main = do (do do-the-major stuff here putStr "File created...") `catch` (\_ -> show the error) getLine -- look ma, no <- return ()
now, your problem is almost certainly with buffering. in the main do, put
hSetBuffering stdout NoBuffering hSetBuffering stdin NoBuffering
you'll need to import System.IO to get these.
-- Hal Daume III | hdaume@isi.edu "Arrest this man, he talks in maths." | www.isi.edu/~hdaume
-----Original Message----- From: haskell-cafe-admin@haskell.org [mailto:haskell-cafe-admin@haskell.org] On Behalf Of Alexandre Weffort Thenorio Sent: Tuesday, August 12, 2003 4:17 PM To: haskell@haskell.org Cc: haskell-cafe@haskell.org Subject: Help with Exceptions on I/O
I have a program which creates textfiles out of other files. Since the program is runned from windows I output some text strings (Like "File created succefully") and I need to stop the program before it quits so that the user can read the line outputted to know what went on and then he can press ENTER to quit the program.
I managed to do this fine if no error occurs but when a error occurs I am having problems.
The code goes like that
main :: IO() main =catch (do Do all the needed stuff here putStr "File created Successfully. Press RETURN to quit" dummy <- getLine --Halts the program so the user can read the above line) putStr "Exiting now..." --needed since I can't finish a do function with the "dummy<- getLine" line) (\_ -> do putStr "\nFile not found. Press RETURN (ENTER) to quit." dumb <- getLine putStr "\nExiting...")
So when the program runs, if the input file is found the program writes successfull creation of file but if the file doesn't exist, after the user gives the input filename and press enter, the program creates a new line and Halts (Probably because of the getLine function) without writing out anything, then when the user press ENTER again it writes the line at the first putStr (File not...), then writes the next putStr line under it (Exiting...) and exits. I don't know why it doesn't wirte the first line, halts and then when user press enter it writes the second and quits.
Can anybody help me as I am not very familiar with exception and catches.
Another question I have is: Is there any other function rather than getLine that halts a program and continues when a user press any key (Instead of ENTER) and besides this is an ugly code since getLine wasn't made for that but I couldn't find anything else myself.
Thank you in advance.
Best Regards
Alex _______________________________________________ Haskell mailing list Haskell@haskell.org http://www.haskell.org/mailman/listinfo/haskell
participants (2)
-
Brandon Michael Moore -
Hal Daume