I have a pretty simple code:
void LookupName( string tname ){ //Checks for name in records.txt and enters it if it does not exist
//Variables
string fname; //Used to store the name
string throwaway; //Used to dispose of extra data
bool found = false; //Used to track if a name is found in the file
//objects
fstream file ( STATS_FILE ); //Open the file
if (file.is_open()) {
while ( !file.eof() ) {
getline (file, fname, DELIM); //Fill name
getline (file, throwaway, '\n'); //throw away rest of line
cout << "Found: " << fname << " tname: " << tname << '\n';
Pause();
if ( fname == tname ) { //Otherwise, continue
cout << "Found: " << fname << " tname: " << tname << '\n';
Pause();
found = true;
}
}
if ( found == false ) { //if the name is not found
//Reopen the file so that we can write to it
file.close();
file.open( STATS_FILE, fstream::in | fstream::out | fstream::app );
cout << "Not found" <<endl;
Pause();
file << tname << ",0,0\n"; //Add it to the file with 0 wins and losses
}
//Cleanup
file.close();
}
}
This works, but if you notice at the bottom when I check to see if the name is found, I have to close and re-open the file.
The follow will not work for some reason:
void LookupName( string tname ){ //Checks for name in records.txt and enters it if it does not exist
//Variables
string fname; //Used to store the name
string throwaway; //Used to dispose of extra data
bool found = false; //Used to track if a name is found in the file
//objects
fstream file ( STATS_FILE, fstream::in | fstream::out | fstream::app ); //Open the file
if (file.is_open()) {
while ( !file.eof() ) {
getline (file, fname, DELIM); //Fill name
getline (file, throwaway, '\n'); //throw away rest of line
cout << "Found: " << fname << " tname: " << tname << '\n';
Pause();
if ( fname == tname ) { //Otherwise, continue
cout << "Found: " << fname << " tname: " << tname << '\n';
Pause();
found = true;
}
}
if ( found == false ) { //if the name is not found
cout << "Not found" <<endl;
Pause();
file << tname << ",0,0\n"; //Add it to the file with 0 wins and losses
}
//Cleanup
file.close();
}
}
I'm curious to know why it doesn't work in the second example as it seems more efficient to open the file only 1 time with the correct flags, do what I need to do and close it.
I have a feeling this might have something to do with the position of the cursor, I have atttempted to use something like file.seekg(0) and file.seekg(0, ios_base::beg) but they don't seem to work as advertised (or I just mis understood the advertisement).
any input would be appreciated.
Edit: The couts were for debugging.
Edit 2: I Suppose I should emphasize the question a little more.
The problem is that the second example does not write to the file where as the first one does. I understand that there might be some concern regarding the !file.eof() condition but in this instance I don't care if it runs an extra time as it wont negatively effect the outcome (additionally, the text file that is being read from has been formatted correctly so that this won't happen).
Edit 3: I created a very small program that ran:
//Testing bs
fstream file("Test.txt", fstream::in | fstream:: out | fstream::app );
string temp;
//ClearScreen
system(CLEAR_SCREEN);
file << "Line one\n";
getline(file, temp);
file << temp;
file << "Line two\n";
Pause();
return Menu;
Only the first line is written to the file. I'm betting that getline is changing the mode of the stream which is why it is unwritable afterwords.
Final Edit: after a bunch of research it appears that in the above situation, re-opening the file is the best resolution. Ultimate,y the issue is in the use of getline() vs file.getline(). I'd have to rewrite far too much of the 1000 lines of the program to do it "correctly." Moral of the story? if you are having this issue, spend some time researching the difference between istream::getline and getline(string) and learn to identify when you are going to use which so that you don't get stuck in this situation. Fortunately, it's not imperative I fix it now, but it might be for others in the future.