Input/Output file

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Input/Output file

Sara Kenedy
Hi all,

I have a module Test.hs:

module Test where
import IO
readMsgFile = do
        putStr "Input file: "
        ifile <- getLine
        putStr "Output file: "
        ofile <- getLine
        s <- readFile ifile
        writeFile ofile (test s)

--test function
test :: String -> String
test s
        |s == "true"  = "True"
        |s == "false" = "False"
        |otherwise = []

Then, I run
*Test> readMsgFile
Input file: test1.txt
Output file: result1.txt

The content of test1.txt is string "true". So I expect the result in
"result1.txt" is "True"
But, open file "result1.txt", it displays empty.


I do not know why it does not return correct answer. If you don't
mind, please share with me. Thanks a lot.

S.
_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re: Input/Output file

Stefan Holdermans
Sara,

> --test function
> test :: String -> String
> test s
> |s == "true"  = "True"
> |s == "false" = "False"
> |otherwise = []
>
> Then, I run
> *Test> readMsgFile
> Input file: test1.txt
> Output file: result1.txt
>
> The content of test1.txt is string "true". So I expect the result in
> "result1.txt" is "True"
> But, open file "result1.txt", it displays empty.


Could it be that the input file has a trailing return or something  
similar?

You could easily test this by making a small change to your test  
function:

   test s
         | ...
         | ...
         | otherwise = error $ "The file contains: " ++ s

HTH,

   Stefan
_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re: Input/Output file

Neil Mitchell
In reply to this post by Sara Kenedy
Hi Sara,

Just a guess, but your input file is probably "true\n" - with a
trailing newline. In fact, many text editors won't let you create a
file without that training newline.

Thanks

Neil

On 7/3/06, Sara Kenedy <[hidden email]> wrote:

> Hi all,
>
> I have a module Test.hs:
>
> module Test where
> import IO
> readMsgFile = do
>         putStr "Input file: "
>         ifile <- getLine
>         putStr "Output file: "
>         ofile <- getLine
>         s <- readFile ifile
>         writeFile ofile (test s)
>
> --test function
> test :: String -> String
> test s
>         |s == "true"  = "True"
>         |s == "false" = "False"
>         |otherwise = []
>
> Then, I run
> *Test> readMsgFile
> Input file: test1.txt
> Output file: result1.txt
>
> The content of test1.txt is string "true". So I expect the result in
> "result1.txt" is "True"
> But, open file "result1.txt", it displays empty.
>
>
> I do not know why it does not return correct answer. If you don't
> mind, please share with me. Thanks a lot.
>
> S.
> _______________________________________________
> Haskell-Cafe mailing list
> [hidden email]
> http://www.haskell.org/mailman/listinfo/haskell-cafe
>
_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re: Input/Output file

David House
In reply to this post by Stefan Holdermans
On 03/07/06, Stefan Holdermans <[hidden email]> wrote:
>    test s
>          | ...
>          | ...
>          | otherwise = error $ "The file contains: " ++ s

Or perhaps,

...
| otherwise = error $ "The file contains: " ++ show s

Then all the newlines and other control characters will be printed in
their escaped form, making it more obvious.

--
-David House, [hidden email]
_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe