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
27 views
asked 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 by rawn

Related questions

0 votes
1 answer 33 views
0 votes
1 answer 104 views
0 votes
1 answer 47 views
47 views asked by allison Sr Member (1,490 points)
0 votes
1 answer 57 views
...