C Books Guide and List
C++ Books Guide and List
Best Java Books

3,749 questions

4,049 answers

114 comments

10,705 users

How can I open a stream in binary mode in c++?

0 votes
26 views
asked Jan 4, 2014 by anna

1 Answer

0 votes
 
Best answer

Use std::ios::binary.

Some operating systems differentiate between text and binary modes. In text mode, end-of-line
sequences and possibly other things are translated; in binary mode, they are not. For example, in text mode under Windows, "\r\n" is translated into "\n" on input, and the reverse on output.
 
To read a file in binary mode, use something like this:
 
#include
#include
#include
void readBinaryFile(const std::string& filename)
{
std::ifstream input(filename.c_str(), std::ios::in | std::ios::binary);
char c;
while (input.get(c)) {
...do something with c here...
}
}
 
Note: input >> c discards leading whitespace, so you won't normally use that when reading
binary files.

 

answered Jan 4, 2014 by rawn

Related questions

0 votes
1 answer 32 views
0 votes
1 answer 103 views
103 views asked Jul 10, 2011 by Smita Advisor (7,140 points)
0 votes
1 answer 47 views
47 views asked Jul 20, 2011 by allison Sr Member (1,490 points)
0 votes
1 answer 57 views
...