Question

I am so stuck. I'm trying to create an ascii text file that stores the values of a .PGM file.

I can read the file fine, the header is in ascii and it reads perfectlly.

But the problem is, I need to read everything after the 16byte header as ascii and output it as atext file for example:

0 235 23 02 255 

etc. This is instead of the binary representation, which looks similar to:

   ÐHHb{{ 

So here's my code for basic a basic read of the file:

void readTxt(char * fileName){
std::ifstream inFile(fileName);

unsigned int * cData = 0;

int rows = 1943;
int cols = 1365;

try{
    if(inFile.is_open()){
        cData = new unsigned int[rows*cols];    
        int i = 0;

        while(inFile.good()){
            if(i > rows*cols -1) break;
            inFile >> cData[i]; i++;
        }
    }
    else throw "Could not open file\n";

    std::cout <<"done\n";

    inFile.close();
} 
catch(const char * e){ std::cout << "Err"; }

delete[] cData;

}

But I'm completely stumped. What do I do with the data? I'm not sure, but I think it's stored as chars with spaces,

so how would identify say char[3] = "255" as an int? or that just char[0] = "0" is an int. They have varying length. Ah I'm so confused. Hate to sound like a novice but I'd hate to waste anymore time!

Thanks

No correct solution

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top