Searching through a file and appending

I've been working this problem for a while. I'm trying to search a file for a name, and if it finds that name, it just ignores it and continues the program, but if it doesn't find the name, it will append onto the end of the file the player's name, along with two 0's. This is what I have:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
 void searchandAppend () {
   ifstream in ("highscores.dat");
   string line;
   while (getline (in,line) && line.find(playerName) == string::npos);
   in.close();
   if (playerName == line) {
      return;
   }
   else {
      ofstream out ("highscores.dat",ios::app);
      out << playerName<< "0" <<"0" << endl;
      out.close();
    }
 }


It compiles just fine, but when it goes to append onto the end of the list of names in "highscores.dat" the first "if (in)" case comes back as true. I'm not sure how to exactly go about this.

Edit: I've been playing around with it. The edited code will now return that the player name is found, but won't print out the playerName
Last edited on
Bump?
Never mind, I figured it out on my own. I ended up with this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
void searchandAppend() {
   string playerName;
   cout << "Please enter your name:";
   cin >> playerName;
   ifstream in ("highscores.dat");
   string line;
   while (getline (in,line) && line.find(playerName) == string::npos);
   in.close();
   if (in) {
      return;
   }
   else {
      ofstream out ("highscores.dat",ios::app);
      out << playerName<< " 0 0" << endl;
      out.close();
    }
 }
Topic archived. No new replies allowed.