Whats happening with this filestream pointer?
Hi, I alredy got it to work, but I did things that I figured out by debugging it, and I dont understand very well what is happening..
So..I trying to read a file header( its a ppm image file), here is a commom one:
P3
# example comment
512 512
255
The problem is I have to avoid the comments, since it can be putted anywhere, until it have a '#' first, and is no long then a line..
Ok, so Im using a getline to skip it, the problem is after the getline I dont know whats really happening with the pointer, so i have to use seekg to get the right stuff, and I dont know why...
Heres my code:
...
int width, height;
int MPV;//max pixel value
char MV[2];//the first 2 ascii values
...
void LoadPNM::ReturnPNM_header(){
int countspaces=0;
char skipcomment[200];
char comm;
int auxpos=0;
for( int i=0; countspaces<3; i++ ){//until 3 spaces
readPNM>>comm;
if(comm != '#'){//if is not a comment
auxpos=readPNM.tellp();
readPNM.seekp(auxpos-1);//the comm moves the pointer also, so back one
switch (countspaces){
case 0:
readPNM >> MV[0] >> MV[1];//heres ok
countspaces++;
break;
case 1:
readPNM >> width >> height ;//heres ok just because the seekg(auxpos-2) after the getline
countspaces++;
break;
case 2:
auxpos=readPNM.tellp();
readPNM.seekp(auxpos-1);//why?
readPNM >> MPV ;
countspaces++;
break;
}//F switch countspaces
}//F if isnt a comment
else{
readPNM.getline(skipcomment, 200, (char)10);//if is a comment, skip the line
auxpos=readPNM.tellp();
readPNM.seekp(auxpos-2);//why?
//error=true;
}
}//F for countspaces <=3
}
I just figure out how much I have to back the pointer by debbuging it, i dont know why it get in those positions...
Its like getline are jumping 2 positions after the newline
Also after read width and height, the pointer jumps to the '2' and just put '55'(the correct is '255') on the MPV variable( that happens without the corrections with the seekg...)
So, anyone can explain me whats happening? I dont know if Im being clear, my english is terrible.
Icebone1000
Junior Poster in Training
50 posts since Jan 2008
Reputation Points: 10
Solved Threads: 0
This should do what you're looking to do. For some reason there is a small issue with re-using the string stream for the 4th line (so I made a new one, I left the code that doesn't work commented), but I've started a thread about why that is.
void SkipComments(const string &Filename)
{
/* test.txt
P3
# example comment
512 512
255
*/
ifstream fin(Filename.c_str());
if(fin == NULL)
cout << "Cannot open file." << endl;
char MV[2];//the first 2 ascii values
int width, height;
int MPV;//max pixel value
string line;
getline(fin, line); //get first line
MV[0] = line[0];
MV[1] = line[1];
cout << "MV[0] = " << MV[0] << endl;
cout << "MV[1] = " << MV[1] << endl;
line = "";
getline(fin, line); //get second line
string FirstChar = line.substr(0, 1);
if(FirstChar == "#")
cout << "Skipped this line." << endl;
line = "";
getline(fin, line); //get third line
stringstream LineStream;
LineStream << line;
LineStream >> width >> height;
cout << "Width = " << width << endl;
cout << "Height = " << height << endl;
LineStream.str("");
line = "";
getline(fin, line); //get fourth line
/*
LineStream.str("");
LineStream << line;
LineStream >> MPV;
*/
stringstream LineStream2;
LineStream2 << line;
LineStream2 >> MPV;
cout << "MPV = " << MPV << endl;
/*
//this is how you would read the rest of the lines in the file
vector<string> Lines;
while(getline(fin, line))
{
Lines.push_back(line);
}
*/
}
Dave
daviddoria
Posting Virtuoso
1,996 posts since Feb 2008
Reputation Points: 437
Solved Threads: 204
daviddoria
Posting Virtuoso
1,996 posts since Feb 2008
Reputation Points: 437
Solved Threads: 204
So Im kind confused...
The thing is that after read some piece of the file to the variables it gets on that 'end-of-file' state, is that right?
So what exactly should someone do to avoid it? Just back 2 positions? Or theres no way to know exactly where the pointer will get...?
Icebone1000
Junior Poster in Training
50 posts since Jan 2008
Reputation Points: 10
Solved Threads: 0