Your problem is that you are mixing signed and unsigned variables. getc returns a signed integer, but your variable c is an unsigned char. getc doesn't ever return 255, it returns -1 (EOF) on end of file, but as I mentioned earlier, -1 (EOF) when assigned to an unsigned char becomes 255. To illustrate this, execute the following code:
Code:
cout << (int) (unsigned char) EOF
<< "\n";
cout << (int) (char) EOF
<< "\n";
To fix your code for reading from your input file, change c to a signed char (not sure why you need it to be a static variable, but I'm sure you have your reasons), then change your while statement so your code looks like the following:
Code:
static char c;
.
.
.
while ((c = getc(stream)) != EOF)
{
.
.
.
I didn't delve too much in detail into the rest of your code, but there are some potential pitfalls depending on what your input is & without seeing your input I can't tell for sure. As an example looking at your code such as:
Code:
NoOfBytesInHeader = (int)HeaderBuffer[8] + (int) HeaderBuffer[9] * pow(2,8);
It looks like you could have values < ascii 32 (' ') written to your data file, if that's the case, then you really have a binary file, not a text file which could cause potential trouble. If on the other hand your data values (such as HeaderBuffer[8] and HeaderBuffer[9]) are going to be ascii numeric chars ('0'..'9'), then casting them to ints isn't going to give you the correct values, you need to convert them to their corresponding integer values ('0' to 0 and '1' to 1 etc.)
Also, just as a friendly reminder, it helps a lot to read your code if you put the [ignore]
[/ignore] tags around it.
Good luck.