Thread: Problem searching for a string in a text file. Help?

  1. #1
    Seven years? civix's Avatar
    Join Date
    Jul 2002
    Posts
    605

    Problem searching for a string in a text file. Help?

    Alright, I've been searching and digging through threads for a couple of hours now, and haven't really found anything I can use/understand.

    Code:
    ifstream choice_one_op_in ( "C:\\Program Files (x86)\\Electronic Arts\\Crytek\\Crysis\\Game\\Config\\diff_easy.cfg");
          string token; 
          string line = "g_godMode = 1";
          while(!choice_one_op_in.eof())
          {
              getline(choice_one_op_in,token);
    
              if (token.find(line, 0))
                  {
    	        cout << "The program found the word: " << line << endl; 
                   } 
          }
    The idea is that the program searches through a configuration file looking for the string. If it's found, it needs to return an error message.

    Can anyone explain to me what I'm doing wrong?

    Thanks in advance.
    Last edited by civix; 11-03-2010 at 03:34 AM. Reason: Loop closing bracket, indentation.
    .

  2. #2
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,130
    Can anyone explain to me what I'm doing wrong?
    Can you explain what makes you think you're doing something wrong? Something has to happen when you run this program. What is it and what would you like to be different? Does it crash, does it produce wrong results?

    For starters, the result of string::find should be compared to string::npos to see if something was found or not.
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  3. #3
    Seven years? civix's Avatar
    Join Date
    Jul 2002
    Posts
    605
    The issue is that it just doesn't work. It produces no results.

    I know I'm missing something, as tonight is the first time I've picked up a compiler in at least a couple of years.

    Can you give me an example as to how to perform the comparison you mentioned? Should I be using strcmp()?

    EDIT:

    Or.. is this right?

    Code:
              if (token.find(line, 0) != string::npos)
    Last edited by civix; 11-03-2010 at 05:11 AM.
    .

  4. #4
    Seven years? civix's Avatar
    Join Date
    Jul 2002
    Posts
    605
    Made a bit of progress using this:

    Code:
     ifstream choice_one_op_in ( "C:\\Program Files (x86)\\Electronic Arts\\Crytek\\Crysis\\Game\\Config\\diff_easy.cfg", ios::in);
          
          
    
           
          
          while(!choice_one_op_in.eof())
          {
                                        size_t found;
                                        string line = "g_godmode = 1";
                                        string token;
                                        getline(choice_one_op_in,token);
                                        found=token.find(line);
      if (found!=string::npos)
      {
       cout << "Found flag at " << int(found) << endl; 
      } 
    
    } //while closing bracket
    This set up is recognizing that the string exists in the file, but it is not giving me the correct location of the string. Could this be because of multiple rows in the file?
    Last edited by civix; 11-04-2010 at 03:58 AM.
    .

  5. #5
    Seven years? civix's Avatar
    Join Date
    Jul 2002
    Posts
    605
    Got everything settled and converted to a function.

    Code:
    int searchtxt(string arg1)
    {
        int linenumber = 0;
        ifstream check ( "C:\\Program Files (x86)\\Electronic Arts\\Crytek\\Crysis\\Game\\Config\\diff_easy.cfg", ios::in);
        while(!check.eof())
        {
             string token;
             getline(check,token);
             linenumber++; //uses loop to add 1 to the variable for every time getline executes before finding it's mark.
        if (token.find(arg1)!=string::npos)
          {
              cout << "Found flag at line " << linenumber << endl;
              token.erase(0, token.length());
              linenumber = 0; //reset linenumber variable to zero for reuse
              check.close(); //close input filestream
          } //closing if bracket
        } //closing while bracket
    }
    Case closed, thanks!
    .

  6. #6
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    This...
    Code:
    while(!check.eof())
    {
        string token;
        getline(check,token);
        ...
    ...results in processing the last line of the file twice. You should avoid checking EOF to control your loops as it is not set to true until after an attempt to read past the end of the file has occurred. After reading the last line, EOF is still false and you go through your loop one more time even though you are already at the end of the file. Once you're in the loop, you've already committed yourself to processing data... the getline call will fail (EOF would be true at this point since you've now tried to read past the end of the file) but still you're already in the loop and so you continue with all that code needlessly.

    The better way to do this is to check the return result of the getline call. It returns a reference to the stream (the "check" object) which in the context of the while loop's conditional can be looked at as a true/false result pertaining to the state of said stream. That would look like this:
    Code:
    string token;
    while(getline(check,token))
    {
        ...
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  7. #7
    Seven years? civix's Avatar
    Join Date
    Jul 2002
    Posts
    605
    Thanks for the tip, that makes alot of sense.
    Modified my code.
    .

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 04-25-2008, 02:45 PM
  2. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 05:02 AM
  3. A bunch of Linker Errors...
    By Junior89 in forum Windows Programming
    Replies: 4
    Last Post: 01-06-2006, 02:59 PM
  4. Calculator + LinkedList
    By maro009 in forum C++ Programming
    Replies: 20
    Last Post: 05-17-2005, 12:56 PM
  5. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM