Thread: Bizarre behavior of the string::find() function

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827

    Bizarre behavior of the string::find() function

    I have the following function:

    Code:
    string C_replace_operations::generateNewFileStr(const vector<string>& searchStrs, const vector<string>& replaceStrs) {
    
        string new_file_str;
        string buffer_str;
    
        if (searchStrs.size() != replaceStrs.size()) {
           cerr<< "Error! You must pass the same number of searchStrs as replaceStrs!\n"
                  "You passed " << searchStrs.size() << " searchStrs, but you passed " <<
                  replaceStrs.size() << " replaceStrs.\n\n";
           return new_file_str; //an empty string, since there must be the same number of search strings as there are replace strings,
           //though there can be empty replace strings
        }
    
        if (vectorOnlyContainsEmptyStrings(searchStrs)) {
           cerr<< "Error! You must pass at least one non-empty search string!\n"
                  "Duh!\n";
           return new_file_str; //an empty string
        }
    
        if (vectorOnlyContainsEmptyStrings(replaceStrs)) {
           cerr<< "Error! You must pass at least one non-empty replace string!\n"
                  "Duh!\n";
           return new_file_str;
        }
    
        bool found_all_search_strs_in_current_line = true; //in faith...haha
        size_t find_result = 0;
        while (!input_filestream.eof()) {
              getline(input_filestream, buffer_str);
              //cout<< "Just read a line." <<endl;
              //cout<< "buffer_str is: " << buffer_str <<endl;
              if (input_filestream.good()) {
                 for (size_t i = 0; i < searchStrs.size(); i++) {
                     find_result = buffer_str.find(searchStrs.at(i), 0);
                     if (find_result == string::npos) { //meaning the current searchStr was not found in current line
                        cout<< "find_result == string::npos" <<endl;
                        //cin.get();
                        found_all_search_strs_in_current_line = false;
                        break; //since all searchStrs must be contained within the current line to be counted
                     }
                     else
                        cout<< "find_result is: " << find_result <<endl;
                 }
    
                 if (found_all_search_strs_in_current_line) {
                    //cout<< "All search strings were found in current line." <<endl;
                    //cout<< "buffer_str before replacing part of the content is:\n\n" << buffer_str << '\n' <<endl;
                    //cout<< "Press Enter to continue.\n" <<endl;
                    //cin.get();
                    for (size_t i = 0; i < searchStrs.size(); i++) {
                        find_result = buffer_str.find(searchStrs.at(i), 0);
                        if (!replaceStrs.at(i).empty()) {
                           cout<< "searchStrs.at(" << i << ") is:\n\n" << searchStrs.at(i) <<endl;
                           cout<< "replaceStrs.at(" << i << ") is:\n\n" << replaceStrs.at(i) <<endl;
                           cout<< "find_result is: " << find_result <<endl;
                           buffer_str.replace(find_result, searchStrs.at(i).size(), replaceStrs.at(i));
                           replaced_strs = true;
                           //cout<< "Just replaced the string in buffer_str that matches the string\n"
                           //       "searchStrs.at(" << i << ").\n" <<endl;
                           //cout<< "Press Enter to continue.\n" <<endl;
                           //cin.get();
                           //cout<< "searchStrs.at(" << i << ") is the following quoted string, without\n"
                           //       "the quotes:\n\n\"" << searchStrs.at(i) << "\"\n" <<endl;
                           //cout<< "Press Enter to continue.\n" <<endl;
                           //cin.get();
                           //cout<< "The string that replaced the above quoted string is the following\n"
                           //       "quoted string:\n\n\"" << replaceStrs.at(i) << "\"\n" <<endl;
                           //cout<< "Press Enter to continue.\n" <<endl;
                           //cin.get();
                        }
                    }
                 }
    
                 else
                     found_all_search_strs_in_current_line = true; //reset to true - in faith, haha
    
                 new_file_str += buffer_str; //add the current line, modified or not, to the new file's content string
                 new_file_str += '\n';
              }
        }
    
        return new_file_str;
    
    }
    Its supposed to replace certain content of a string read from line which contains all search strings. Basically, each search string in the line which contains is supposed to be replaced by the string at the same index. Search strings and replace strings are each stored inside a vector<string>.

    This function has worked ok up until today, after which I'm getting weird unexpected results. Basically, the string::find() function is returning string::npos when its not supposed to (i.e. when each search string IS contained inside buffer_str), thereby causing an instance of 'std:ut_of_range' to be thrown from string::replace().

    Please take a look at the above code, and see if you can spot any above errors. I've already been through the code several times, but have not seen why it should be doing this. Note that I call the above function only once too.

    See attachment for output. The .txt at the end will need to removed to view the PNG image.

    Thanks.
    Last edited by Programmer_P; 03-08-2011 at 09:18 PM.
    I'm an alien from another world. Planet Earth is only my vacation home, and I'm not liking it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling C in Visual Studio 2005
    By emanresu in forum C Programming
    Replies: 3
    Last Post: 11-16-2009, 04:25 AM
  2. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  3. Troubleshooting Input Function
    By SiliconHobo in forum C Programming
    Replies: 14
    Last Post: 12-05-2007, 07:18 AM
  4. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  5. const at the end of a sub routine?
    By Kleid-0 in forum C++ Programming
    Replies: 14
    Last Post: 10-23-2005, 06:44 PM