search
Login

Categories


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

3.8k questions

4k answers

117 comments

10.7k users

0 votes
29 views
asked 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 rawn

Related questions

0 votes
1 answer 38 views
0 votes
1 answer 106 views
106 views
How can I search for data in a linked list?
asked Smita Advisor 7.1k points 10 23 42
0 votes
1 answer 48 views
48 views
asked allison Sr Member 1.5k points 11 26 40
0 votes
1 answer 61 views
...