bool BODYTYPE::Seek(BODYTYPE * body)
{	
	
	string line;
	string key(body->style);	
	size_t found = ' ';

	fstream file("..\\Data\\BODYTYPE.txt",ios::in);

	if (!file.is_open())
	{ 
		cout << "Unable to open file \n";
		return false;
	}
	
	file.clear();
	file.seekg(0);
	for(;getline(file,line);)
	{
		if((found = line.find(key,0)) == 0 )
		{
			file	>> body->style							
				>> body->doorCount;
			return true;
		}
	}

	if ((found = line.find (key, 0)) != 0 )
		cout << "Style " << key << " not found!" << endl;
		
	
	file.close();
	return false;
}

How ever if I have data in a the file:

Pickup 2
4x4 4
Sedan 4
Convertible 2
Hardtop 4
Hatchback 5
Muscle 0

and i try to seek "Hardtop" it retrieve Hatchback.
It is over seeking.
How can I correct this?

line 28: delete that if statement because if the program gets that far its 100% true that the word was not found. So all you need to do at that point is display the error message.

lines 22 and 23: you have already read the line that begins "Hardtop" on line 18. Lines 22 and 23 just read the next line. What you need to do is use stringstream to split the line that was read on line 18

stringstring str(line);
str >>  body->style >> body->doorCount;

Another way to do it is to change the loop on line 18. The problem with this approach is that it will destroy the values in that structure/class. So if that's not what you want then don't use it.

while( file >>  body->style >> body->doorCount)
{
    if( body->style == key)
          return true;
}

Sweet...
Thanks!!!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.