Reading in a PGM files binary data block using c++

CrazYmonkeY159

Expert Member
Joined
Sep 13, 2007
Messages
2,142
Hi guys

for a school assignment i have to read in the binary data block contained in a .pgm file using c++

i know how to read in the whole .pgm using read() with a file stream object;

if you dont know a pgm is structured like this

----------------
PN (where N is a number)
# Comment
# Comment
Int Int
Int
_binary data block_
------------------

how do i read in the 1st two Integers and the binary data block? ive been struggling on this one for days :(
 

CrazYmonkeY159

Expert Member
Joined
Sep 13, 2007
Messages
2,142
heres a code snippet

ifstream pic;
char *buffer;
int nRow, nCol, length;
pic.open("mypic.pgm", ios::binary);

pic.seekg(0,ios::end);
length = pic.tellg();
pic.seekg(0,ios::beg);
buffer = new char[length];
pic.read((char*)buffer,length);
pic.close();

cout.write(buffer,length);
ofstream os("output");
os.write(buffer,length);
os.close();
return 0;

so this reads in everything contained in mypic.pgm into buffer but i want to read in only binary data contained after the 3rd integer which i mentioned above. how to i set my get pointer to after the 3rd int value ?
 

Gnome

Executive Member
Joined
Sep 19, 2005
Messages
7,208
*sigh* Your teachers structure is very bad.

Post an example .pgm file so I can see if there is any logic to this structure.
 
Top