Runtime error while feeding a binary to stdin

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

Runtime error while feeding a binary to stdin

Manuel Vázquez Acosta
Hi all,

I'm quite new to Haskell.  While following the "Real World Haskell" and
doing some experimentation I came up with a anoying situation:

Trying to read data from stdin it seems that binary data is not
allowed.  A simple "copy" program:


   -- file: copy.hs
   import System.IO

   main = do
      input <- hGetContents stdin
      hPutStr input

Fails when I run it like:

   $ ghc copy.hs
   $ ./copy < input > output
   copy: <stdin>: hGetContents: invalid argument (invalid byte sequence)

input contains binary data.  In fact of all the following programs only
the first works with binary data:

  copy:: IO ()
  copy = do
    bracket (openBinaryFile "input" ReadMode) hClose $ \hi -> do
      bracket (openBinaryFile "ouput" WriteMode) hClose $ \ho -> do
        input <- hGetContents hi
        hPutStr ho input


  copy2:: IO ()
  copy2 = do
    -- Doesn't work with binary files
    source <- readFile "input"
    writeFile "output" source


  copy3:: IO ()
  copy3 = do
    -- Doesn't work with binary files either
    interact (map $ \x -> x)


  copy4:: IO ()
  copy4 = do
    input <- hGetContents stdin
    hPutStr stdout input


But I lost any chance of piping and/or using '<', '>' in the shell.

Best regards,
Manuel.
_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: Runtime error while feeding a binary to stdin

Theodore Lief Gannon
Those System.IO functions *are* String-specific. Try the equivalents from Data.ByteString:


On Tue, Jan 10, 2017 at 2:46 PM, Manuel Vázquez Acosta <[hidden email]> wrote:
Hi all,

I'm quite new to Haskell.  While following the "Real World Haskell" and
doing some experimentation I came up with a anoying situation:

Trying to read data from stdin it seems that binary data is not
allowed.  A simple "copy" program:


   -- file: copy.hs
   import System.IO

   main = do
      input <- hGetContents stdin
      hPutStr input

Fails when I run it like:

   $ ghc copy.hs
   $ ./copy < input > output
   copy: <stdin>: hGetContents: invalid argument (invalid byte sequence)

input contains binary data.  In fact of all the following programs only
the first works with binary data:

  copy:: IO ()
  copy = do
    bracket (openBinaryFile "input" ReadMode) hClose $ \hi -> do
      bracket (openBinaryFile "ouput" WriteMode) hClose $ \ho -> do
        input <- hGetContents hi
        hPutStr ho input


  copy2:: IO ()
  copy2 = do
    -- Doesn't work with binary files
    source <- readFile "input"
    writeFile "output" source


  copy3:: IO ()
  copy3 = do
    -- Doesn't work with binary files either
    interact (map $ \x -> x)


  copy4:: IO ()
  copy4 = do
    input <- hGetContents stdin
    hPutStr stdout input


But I lost any chance of piping and/or using '<', '>' in the shell.

Best regards,
Manuel.
_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners


_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: Runtime error while feeding a binary to stdin

Manuel Vázquez Acosta
Theodore Lief Gannon <[hidden email]> writes:

Hi Theodore,

Thanks, I didn't know about those functions.

Best regards,
Manuel.

> Those System.IO functions *are* String-specific. Try the equivalents from
> Data.ByteString:
>
> http://hackage.haskell.org/package/bytestring-0.10.8.1/docs/Data-ByteString.html#g:29
>
> On Tue, Jan 10, 2017 at 2:46 PM, Manuel Vázquez Acosta <[hidden email]>
> wrote:
>
>> Hi all,
>>
>> I'm quite new to Haskell.  While following the "Real World Haskell" and
>> doing some experimentation I came up with a anoying situation:
>>
>> Trying to read data from stdin it seems that binary data is not
>> allowed.  A simple "copy" program:
>>
>>
>>    -- file: copy.hs
>>    import System.IO
>>
>>    main = do
>>       input <- hGetContents stdin
>>       hPutStr input
>>
>> Fails when I run it like:
>>
>>    $ ghc copy.hs
>>    $ ./copy < input > output
>>    copy: <stdin>: hGetContents: invalid argument (invalid byte sequence)
>>
>> input contains binary data.  In fact of all the following programs only
>> the first works with binary data:
>>
>>   copy:: IO ()
>>   copy = do
>>     bracket (openBinaryFile "input" ReadMode) hClose $ \hi -> do
>>       bracket (openBinaryFile "ouput" WriteMode) hClose $ \ho -> do
>>         input <- hGetContents hi
>>         hPutStr ho input
>>
>>
>>   copy2:: IO ()
>>   copy2 = do
>>     -- Doesn't work with binary files
>>     source <- readFile "input"
>>     writeFile "output" source
>>
>>
>>   copy3:: IO ()
>>   copy3 = do
>>     -- Doesn't work with binary files either
>>     interact (map $ \x -> x)
>>
>>
>>   copy4:: IO ()
>>   copy4 = do
>>     input <- hGetContents stdin
>>     hPutStr stdout input
>>
>>
>> But I lost any chance of piping and/or using '<', '>' in the shell.
>>
>> Best regards,
>> Manuel.
>> _______________________________________________
>> Beginners mailing list
>> [hidden email]
>> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
>>
> _______________________________________________
> Beginners mailing list
> [hidden email]
> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners