binary file input with cin

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Clemens Park

    binary file input with cin

    Hi,
    I am working on a compressor/decompressor for files,
    and it works great at the moment with text files, but not great all with
    binary files.
    The problem is that it reads in binary files as text files, therefore
    resulting in incorrect compression(it will compress 0.x% instead of around
    97% for a certain file).
    I am wondering if there is a certain function for cin that allows to
    recognize an input file as binary. I am working in a UNIX environment, so
    I pass in input files through standard input in the following format:
    ../a.out < testfile > testfile.cmp
    where a.out is the executable file, testfile is the input file, and
    testfile.cmp is the compressed file.

    Hexdump was mentioned together with binary, but I don't get how it will be
    used at all(and I know that I don't pipe the output of hexdump to a.out,
    since I ran my program and the sample program provided(that works the
    correct way) in the syntax mentioned above.

    Any ideas?
  • David B. Held

    #2
    Re: binary file input with cin

    "Clemens Park" <ydpark@ualbert a.ca> wrote in message
    news:1064107146 .986982@proxy2. srv.ualberta.ca ...[color=blue]
    > [...]
    > I am wondering if there is a certain function for cin that
    > allows to recognize an input file as binary.
    > [...][/color]

    I believe you want to open your istream in ios_binary
    mode. Check your compiler docs for the exact syntax.

    Dave


    Comment

    • David B. Held

      #3
      Re: binary file input with cin

      "David B. Held" <dheld@codelogi cconsulting.com > wrote in message
      news:bkivj5$1jj $1@news.astound .net...[color=blue]
      > "Clemens Park" <ydpark@ualbert a.ca> wrote in message
      > news:1064107146 .986982@proxy2. srv.ualberta.ca ...[color=green]
      > > [...]
      > > I am wondering if there is a certain function for cin that
      > > allows to recognize an input file as binary.
      > > [...][/color]
      >
      > I believe you want to open your istream in ios_binary
      > mode. Check your compiler docs for the exact syntax.[/color]

      Sorry, I forgot that you are dealing with cin. In that case,
      I think istream::read() is what you are looking for. And
      the flag I was talking about is ios::binary. This link might
      be helpful:



      Dave


      Comment

      • Kevin Goodsell

        #4
        Re: binary file input with cin

        David B. Held wrote:
        [color=blue]
        > "Clemens Park" <ydpark@ualbert a.ca> wrote in message
        > news:1064107146 .986982@proxy2. srv.ualberta.ca ...
        >[color=green]
        >>[...]
        >>I am wondering if there is a certain function for cin that
        >>allows to recognize an input file as binary.
        >>[...][/color]
        >
        >
        > I believe you want to open your istream in ios_binary
        > mode. Check your compiler docs for the exact syntax.
        >[/color]

        ios::binary, or (equivalently) ios_base::binar y.

        -Kevin
        --
        My email address is valid, but changes periodically.
        To contact me please use the address from a recent posting.

        Comment

        • Clemens Park

          #5
          Re: binary file input with cin

          Thanks Kevin and David,
          I personally looked this up for a few hours too, but the problem is that I
          don't know how to make the program recognize binary files. Maybe I haven't
          googled enough. How does cin and ios::binary go together?
          Is there an istream command that sets c++ to read in the file, then allows
          me to use plain and normal cin.get(c)?
          I don't know if i stated this before, but I must use cin for the input, and
          also I use g++ for compiling i UNIX.
          An example of the use of ios::binary and cin together would be very very
          appreciated.

          Thanks.
          Clemens


          Kevin Goodsell <usenet1.spamfr ee.fusion@never box.com> wrote in
          news:a_7bb.619$ vS.400@newsread 3.news.pas.eart hlink.net:
          [color=blue]
          > ios::binary, or (equivalently) ios_base::binar y.
          >
          > -Kevin[/color]

          Comment

          • David B. Held

            #6
            Re: binary file input with cin

            "Clemens Park" <ydpark@ualbert a.ca> wrote in message
            news:1064115518 .909316@proxy2. srv.ualberta.ca ...[color=blue]
            > [...]
            > How does cin and ios::binary go together?[/color]

            Technically, I believe you can reopen stdin, but I don't think
            that's the right way to go.
            [color=blue]
            > Is there an istream command that sets c++ to read in the
            > file, then allows me to use plain and normal cin.get(c)?
            > [...][/color]

            I believe the istream::read() function will do an unformatted
            read on the stream, which should be what you want to do.

            Dave


            Comment

            Working...