binary file input with cin

C

Clemens Park

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?
 
D

David B. Held

Clemens Park said:
[...]
I am wondering if there is a certain function for cin that
allows to recognize an input file as binary.
[...]

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

Dave
 
D

David B. Held

David B. Held said:
Clemens Park said:
[...]
I am wondering if there is a certain function for cin that
allows to recognize an input file as binary.
[...]

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

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:

http://www.cplusplus.com/ref/iostream/

Dave
 
K

Kevin Goodsell

David said:
[...]
I am wondering if there is a certain function for cin that
allows to recognize an input file as binary.
[...]


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

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

-Kevin
 
C

Clemens Park

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
 
D

David B. Held

Clemens Park said:
[...]
How does cin and ios::binary go together?

Technically, I believe you can reopen stdin, but I don't think
that's the right way to go.
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 believe the istream::read() function will do an unformatted
read on the stream, which should be what you want to do.

Dave
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top