Hey All. I'm trying to figure out how to search for a string within my file and if the string exists, I want to get whatever follows it. Here's an example:

HighScores.txt
----------------------------
Devin: 202
Corey: 185
Nick: 315
Patrick: 112
----------------------------

If my program opens the file, I'd like for it to search for a string ("Devin") and then get the number following it (202).

if ((offset = line.find(search, 0)) != string::npos)

can search for the string but I don't know how to get the number following it.

ideas?

Thanks for your time :)

Here's what I have so far. It gets all the data in the txt file and prints it out.

string line;
ifstream tmpStream = fc.getInFile(); // gets the file thats is controlled by another class.
while(!tmpStream.eof()) {
	getline(tmpStream, line);
	cout << line << endl;
}

tmpStream.close();

Gettin close!

string line;
	string tmp;
	ifstream tmpStream = fc.getInFile();
	while (!tmpStream.eof()) {
		tmpStream >> tmp;
		if (tmp == "Devin") {
			cout << "got it" << endl;
			break;
		} else {
			cout << "darn" << endl;
		}
	}
}

LOL! I was having a huge brainfart apparently...

bool hasHighScore = false;
	string line;
	string tmp;
	ifstream tmpStream = fc.getInFile();
	while (!tmpStream.eof()) {
		tmpStream >> tmp;
		if (tmp == name) {
			int score;
			tmpStream >> score;
			cout << "Your highest score was: "<< score << endl;
			hasHighScore = true;
			break;
		}
	}
	if (!hasHighScore) {
		cout << "You don't have a high score yet!" << endl;
	}
	tmpStream.close();
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.