ifstream seekg error

May 9, 2010 at 5:08am
Hello,

I want to scan for some strings in a file.
I open the file using ifstream, read the line with getline and seek the string with string::find.

The scan of the first string is ok, However, for the second string I need to position the pointer to the beginning of the file. I use seekg(0,ios::beg) but this doesnt seem to work. When I ask the pointer position with tellg(), it tells me eithr nan, or -1.

I do not want to close and open for each string of course.

I asked the help of an experienced c++ programmer, so I do not think I am doing something silly but he also cant find. We decided to ask for your help...

Thank you very much for reading. Kind regards, zuheyr
May 9, 2010 at 5:17am
We might need your code to debug it.

-Albatross
May 9, 2010 at 9:38am
Hello,

Thank you for your reply. I copied the (hopefully) relevant part of the code. Since I am not experienced in reporting problems, I apologize if it is too long. Kind regards, Zuheyr
================================================================

bool ACase::nanCheck(string path, string& reasonSubcaseFail)
{

stringstream ss;
ss << path << "/" << trafOutputFile ;

//opening the Traf Output file

ifstream file (ss.str().c_str());

if (!file.is_open())
{
stringstream Where,Err;
Where << __FILE__ << "::" << __FUNCTION__ << " at line " << __LINE__;
Err << "Could not open file " << ss.str() << endl;
reasonSubcaseFail = "Could not find Traf output file" ;
return false ;
}
cout << "Hello I am here before searching first INFINITY" << endl ;

if (searchFile(file,"Infinity"))
{
reasonSubcaseFail = "There is Infinity";
return false;
}

// Everything ok up to here

cout << "Hello I am here before searching nan" << endl ;

if (searchFile(file,"NaN"))
{
reasonSubcaseFail = "There is NaN";
return false;
}
cout << "Hello I am here before searching SECOND nan" << endl ;


if (searchFile(file,"nan"))
{
reasonSubcaseFail = "There is nan";
return false;
}
if (searchFile(file,"infinity"))
{
reasonSubcaseFail = "There is Infinity";
return false;
}

file.close();

return true;
}

bool ACase::searchFile(ifstream& File, std::string Label)
{
cout << "traf output file" << trafOutputFile << endl;
string line ;
File.seekg (0);
// File.seekg (0,ios::beg);
cout << "the position of the get pointer" << File.tellg() << endl ;
// File.seekg (0, ios::beg);
while (!File.eof() )
{
getline (File,line);
cout << "The line: \t " << line << endl ;
size_t found;
found=line.find(Label);
if (found != string::npos)
{
return true;
}
}
return false;
}
=============================================
Important part of the result (I was wrong the pointer is always -1)!
==============================================
Hello I am here before searching nan
traf output filetrafout
the position of the get pointer-1
Hello I am here before searching SECOND nan
traf output filetrafout
the position of the get pointer-1
traf output filetrafout
the position of the get pointer-1

May 9, 2010 at 10:39am
I think File.seekg (0,ios::beg); is the correct way to do it (to move to the beginning of the file). Why did you comment it out?
Last edited on May 9, 2010 at 10:40am
May 9, 2010 at 12:59pm
Try adding the instruction File.clear(); when you seek to the start.

That will clear any eof or other error flags from the previous search.
Last edited on May 9, 2010 at 1:00pm
May 9, 2010 at 1:24pm
Thank you for your answers!

m4ster Ir0shi: have tried both, (I saw in an example here in C++ that file.seekg(0) was used), but both are exactly the same.

Galik, I will try that! Thanks again. Regards, Zuheyr
May 10, 2010 at 6:32am
Hello,

Galik, thank you very much. Yes, adding File.clear(); works.
Many thanks and kind regards, Zuheyr
Topic archived. No new replies allowed.