Man kunne lave en lille søge funktion:
std::streampos FindPerson(std::istream &is, int FieldNr, const std::string &Value)
{
is.clear();
is.seekg(0, std::ios::beg);
std::string Line;
std::streampos FilePos = 0;
while(std::getline(is, Line))
{
std::string::size_type Pos = 0;
for(int i = 0; i < FieldNr && Pos != std::string::npos; i++)
{
Pos = Line.find('#');
if(Pos != std::string::npos)
{
Pos++;
Line = Line.substr(Pos);
}
}
Pos = Line.find('#');
Line = Line.substr(0, Pos);
if(Line == Value)
return FilePos;
}
return -1;
}
Så kan man bruge den med:
PersonClass Person;
// Try to find Jensen
std::streampos pos = FindPerson(file, 1, "Jensen");
if(pos != -1)
{ // Found
file.clear();
file.seekg(pos, std::ios::beg);
ReadPerson(file, Person);
std::cout << Person << std::endl;
}
[Redigeret d. 03/12-04 23:34:11 af Bertel Brander]