Hello,
I have a problem in reading p5 PGM file:

ifstream::pos_type _Start, _End, _Size;
        ifstream::off_type _newStart;
        char * memblock;        
        vector<vector<int> > initMatrix ;

	std::ifstream inf;
	inf.open("a.pmg", ios_base::in | ios_base::binary);
	if (inf) {
		std::string line;
		
		for (int i = 0; i<3 ;i++)//skip the first 3 lines that contain the info
		{
			std::getline(inf, line);
		}
		
		_Start = inf.tellg();//starting point just after those 3 lines
		inf.seekg (0, ios::end);
		_End = inf.tellg();
		_Size = (_End - _Start);
		cout<<_Size;//size of the data in the file without the first three lines

		memblock = new char [_Size];
		_newStart = (ifstream::off_type) _Start;//casting _Start to off_type to be passes as offset, to start reading the file after the first three lines, idon't know if that idea is right or not
		inf.seekg (_newStart, ios::beg);
		inf.read (memblock, _Size); // 1st prob., nothing will be red
		cout<<memblock; // nothing in the o/p
////////////////////////////////////////////////////////////////////////////
//this second part is not connected directly to the code but I want to split the data in the memblock which is a matrix of 1024*1024 and push the data into a vector of vectors like so
//this code is right if we are dealing with ASKII file and I have no clue how to make the same idea on the previous problem
		while (std::getline(inf, line)) {
			initMatrix.push_back(vector<int>());

			// Break down the row into column values
			std::stringstream split(line);
			int value;

			while (split >> value){
				initMatrix.back().push_back(value);
			}
		}
	}


	inf.close();

close

1- what is the problem
2- attach an pgm (p5) sample

you can find the description of the problem inside the code, I have done so to make it clearer.
Here u can find a lot of PGM files, you can use anyone of them:http://peipa.essex.ac.uk/ipa/pix/mias/
thanks

Edited 5 Years Ago by salah_saleh: n/a

cout<<memblockcout<<memblock

this line will print the array as string 0 (\0) terminated so if the first element is 0 will print nothing
if you want to make sure that the file was read you can loop through it till first none zero byte or you can initialize it with some value like -1 then check if the data was changed

I have rechecked it, but "memblock" doesn't point to any data. I am sure.

can you try to replace

cout<<memblock;

with

memset(memblock,0,_Size);
inf.read (memblock,_Size); // 1st prob., nothing will be red
for(int i=0;i<_Size;i++)
	cout<<memblock[i]; // nothing in the o/p
This article has been dead for over six months. Start a new discussion instead.