title: C++: File I/O Look for name in file
body: <p>I have this function designed to look for a name in a csv type txt file.</p>
  
  <pre><code>void LookupName( string tname ){ //Checks for name in records.txt and enters it if it does not exist
  //Pointers
  string * names; //Pointer to dynamically created array of names
  
  //Variables
  short numLines = 0; //Stores the number of lines in the file
  string throwaway = " "; //String used to throw away unwanted content from file
  bool found = false; //Used to track if a name is found int he file
  
  //objects
  ifstream file ("records.txt"); //Open the file
  
  
  if (file.is_open()) {
  
      //Count the number of lines
      numLines = count(istreambuf_iterator&lt;char&gt;(file), istreambuf_iterator&lt;char&gt;(), '\n'); 
  
      //Reset cursor position
      file.close();
      file.open("records.txt");
  
      //Create arrays
      names = new string[numLines]; 
  
      //Loop through the file and fill arrays
      for ( short i; i &lt; numLines; i++ ) {
          getline (file, names[i], ','); //Fill name
          getline (file, throwaway ); //Throw the rest of the line away
      }
  
      //Loop through names to find the name that was entered
      for ( short i; i &lt; numLines; i++ ) {
          if (names[i] == tname) { // if the name is found
              found = true; //return true
          }
      }
  
      if ( found == false ) { //if the name is not found
          //This works
          file.close();
          ofstream ofile("records.txt", ios::app);
          ofile &lt;&lt; tname &lt;&lt; ",0,0\n"; //Add it to the file with 0 wins and losses
          ofile.close();
      }
  
      //Cleanup
      file.close();
      delete [] names;
  
  } 
  }
  </code></pre>
  
  <p>For some reason apparently beyond my ability to debug, this doesn't work; however, when I remove the second for loop and combine it with the first one it does work. ( This is how I fully intend to leave it because it doesn't need to be segmented into 3 loops but I'd like to know why for my own education):</p>
  
  <p>Working:</p>
  
  <pre><code>void LookupName( string tname ){ //Checks for name in records.txt and enters it if it does not exist
  //Pointers
  string * names; //Pointer to dynamically created array of names
  
  //Variables
  short numLines = 0; //Stores the number of lines in the file
  string throwaway = " "; //String used to throw away unwanted content from file
  bool found = false; //Used to track if a name is found int he file
  
  //objects
  fstream file ("records.txt"); //Open the file
  
  
  if (file.is_open()) {
  
      //Count the number of lines
      numLines = count(istreambuf_iterator&lt;char&gt;(file), istreambuf_iterator&lt;char&gt;(), '\n'); 
  
      //Reset cursor position
      file.close();
      file.open("records.txt", fstream::in | fstream::out | fstream::app );
  
      //Create arrays
      names = new string[numLines]; 
  
      //Loop through the file and fill arrays
      for ( short i; i &lt; numLines; i++ ) {
          getline (file, names[i], ','); //Fill name
          getline (file, throwaway ); //Throw the rest of the line away
  
          if (names[i] == tname) { // if the name is found
              found = true; //return true
          }
  
      }
  
      if ( found == false ) { //if the name is not found
          //This works
          file &lt;&lt; tname &lt;&lt; ",0,0\n"; //Add it to the file with 0 wins and losses
      }
  
      //Cleanup
      file.close();
      delete [] names;
  
  } 
  }
  </code></pre>
  
  <p>Anyone care to weigh in on why this is happening?</p>
  
  <p>edit: Also, since I'm asking questions ( I was really trying not to ) is there a way to push the cursor back to the top of a file without opening and closing it?</p>
  
  <p>edit 2: Is there a way to tell the program to ignore the remainder of a csv type line, I onyl want the first item on the line for this particular function and I feel using a throwaway string might not be the optimal way.</p>
  
  <p>edit3: updated to use fstream rather than 1 if and 1 of.</p>