ProAnswers.org

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

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, "

" is translated into "
" 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.