3

I have RAW file in grayscale with 8-bit data and I'm trying to read the data using simple program:

#include <fstream>
#include <iostream>
using namespace std;

int main(){
    char fileName[80] = "TEST.raw";
    //char buffer[255];
    ifstream fin(fileName);
    char ch;
    int i = 0;
    while (fin.get(ch)){
        cout << (unsigned int)ch << " ";
        i++;
    }
    cout << endl;
    cout << "i = " << i;
    fin.close();
    cin >> ch;
}

Some of values are higher than some specific value and I'm getting strange output. When i should get something like this:

(...) 34    34    34    36    43    59    88    123    151    166 (...)

i have:

(...) 34 34 34 36 43 59 88 123 4294967191 4294967206 (...)

I guess it's simple problem, but I have no idea how to do it correctly.

Jongware
  • 22,200
  • 8
  • 54
  • 100
Adek
  • 43
  • 1
  • 7

1 Answers1

3

Change your ch variable declaration to

   unsigned char ch;
// ^^^^^^^^

and as recommended in the documentation change fin.get(ch) to

while (fin >> ch)

to fix this behavior.

char values need to be explicitly being signed or unsigned, when you use them as integer numerics. So if a value is bigger than 126 it's interpreted as a negative number. Negative numbers casted to unsigned int will set the high bits of those values.

πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
  • I was trying it, but for now I have an error: 1>d:\programowanie\czytanieraw\czytanieraw\czytanieraw\source.cpp(11): error C2664: 'std::basic_istream> &std::basic_istream>::get(std::basic_streambuf> &,_Elem)' : cannot convert argument 1 from 'unsigned char' to 'char &' 1> with 1> [ 1> _Elem=char 1> ] 1>d:\programowanie\czytanieraw\czytanieraw\czytanieraw\source.cpp(11): fatal error C1903: unable to recover from previous error(s); stopping compilation – Adek May 08 '14 at 17:30
  • 1
    BTW, the type `char` can be implemented by the compiler as `signed`, `unsigned` or `char`. There is no default value, it is implementation specific. – Thomas Matthews May 08 '14 at 17:31
  • @Adek I have updated my answer. You can also get this hint from the [documentation](http://en.cppreference.com/w/cpp/io/basic_istream/get). – πάντα ῥεῖ May 08 '14 at 17:35
  • 1
    @ThomasMatthews you are correct, it's implementation defined. But are you aware of any implementations that *don't* use signed? – Mark Ransom May 08 '14 at 17:37
  • 2
    Yes, embedded systems. I currently have my embedded system environment set up for a default setting of `unsigned` character. The big issue is when the static analysis tools are set up differently than the compiler. – Thomas Matthews May 08 '14 at 17:44
  • Uh, my program for now is working, but I've found different mistake. My `i` variable says that I have 31851 values in 160x200 .raw image. Missing 149 values is number of 32s, what is 'space' in ASCII. Don't ask how I've found it ;) – Adek May 08 '14 at 18:38
  • @Adek are you sure the missing values are 32 and not 13? – Mark Ransom May 08 '14 at 23:00
  • @MarkRansom Yep, I'm sure. I've checked it and for now solved it by using `noskipws` in `while (fin >> noskipws >> ch)`. – Adek May 08 '14 at 23:10