How to read bit by bit in a file

What I would like to do is read
a raw Image in decimal (8 bits, so value 0 - 255)

The code below is something I have started. When I tested it prints out numbers, letters, and ! ? etc.
I understand I am using char, but I should be getting just numbers.
If I do int it prints -858993460 sixteen times.

Am I missing something or am I using the wrong file opener?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
char data[1024];

ifstream inStream;

	inStream.open("test.raw");
	if(inStream.fail())
	{
		cout << "Input file opening failed.\n";
		exit(1);
	}

	while(!inStream.eof())
	{
		inStream >> data[i];
		cout << data[i] << endl;
		i++;
		while(i == 16);
	}
cout << data[i] << endl;

The type of thing you are printing is a char, hence you get apparent garbage. Try this instead:

cout << (int)data[i] << endl;

Enjoy!


[edit] BTW, this isn't reading bit-by-bit. It's byte-by-byte. There is a significant difference. I wanted to answer the bit-by-bit question.
Last edited on
Topic archived. No new replies allowed.