Thread: using fstream.find

  1. #1
    Registered User
    Join Date
    Oct 2007

    using fstream.find

    I am working on a program that will simulate a simple student information system. The program will need to behave as follows:

    a - add a student
    b - display all student information
    c - display a single students information
    d - generate a grade report
    q - quit

    Currently I am working on the "add a student" function. The process I must follow is as follows:

    1. Prompt the user to add the name of a student (Last, First).
    2. The addUser() function will call the checkUser() function to open a text file and search to see if that student exists.
    3. If the user does not exist, a generateID() function is called that will generate a random, five digit ID.
    4. Finally, the addUser() function will prompt the user to enter a series of test scores and homework scores. All information (name, ID, scores) is then written to the student.txt file.

    Simple enough. However, I am trying to test out a few lines of code that will actually search the text file for me.

    I need it to be in two separate functions (hence why I have divided it up), so here is my code as of now:
    #include <iostream>
    #include <fstream>
    #include <string>
    using namespace std;
    int search(void);
    int main (void)
    	cout << search() << endl;
    	return 0;
    int search(void)
    	fstream checkStream;
    	string searchString;
    	string lineOfText;
    	cout << "Please enter a valid name to search for: ";
    	getline(cin, searchString);
"student.txt", ios::in);
    		getline(checkStream, lineOfText);
    		if (checkStream.eof()) break;
    		if (lineOfText.find("searchString", 0) != string::npos)
    			return 1;
    	cout << "Done searching..." << endl;
    	return 0;
    and my student.txt file is as follows:

    Sparrow, Jack
    Turner, Will	
    Snyder, Quinn
    Snyder, Melody
    Basset, Theodore
    However, my code will not output a found message when the string is found. Something is not being passed correctly. Can anyone give me some insight on this problem?


  2. #2
    The larch
    Join Date
    May 2006
    if (lineOfText.find("searchString", 0) != string::npos)
    You are looking for the literal text "searchString", not what user input in variable searchString.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #3
    Registered User
    Join Date
    Jan 2005
    >> if (checkStream.eof()) break;
    It is possible for the call to getline to successfully read in a line but still set the eof bit. This line of code would then cause the last line of the file to be skipped.

    For example, if your student.txt file didn't end with a blank line (the last character of the file is the 'e' at the end of "Theodore"), and you searched for "Basset", then the search would fail.

    I would change the for loop to a while loop, then put the getline into the while control. The call to getline evaluates to true if it is successful, and false if it is not, so it will properly exit the loop when it fails to read any more data:
    while (getline(checkStream, lineOfText))
    If you wanted to leave your code with the same structure, then just change the if statement to this:
    if (!checkStream) break;

Popular pages Recent additions subscribe to a feed