Searching a file for a String Occurence

This is a discussion on Searching a file for a String Occurence within the C++ Programming forums, part of the General Programming Boards category; Originally Posted by laserlight As in it does not actually do a case insensitive comparison? That would be because pStudent ...

  1. #31
    Registered User
    Join Date
    Dec 2008
    Posts
    65
    Quote Originally Posted by laserlight View Post
    As in it does not actually do a case insensitive comparison? That would be because pStudent and pOStudent are pointers, so you are merely comparing addresses. You would need to use strcmp() if you really want to use C-style strings.
    My mind isn't working right today, I shouldn't have woken up to watch the BCS Championship earlier this morning. I need some coffee! Or a nap

    But it goes to show that I am reminded of something every day!

    Code:
    if(!strcmp(pStudent, pOStudent))
    ...
    Thanks!

    Now I will try the std::string route, once I have that figured out i'll post it.

    Cheers!
    Last edited by Phyxashun; 01-09-2009 at 09:52 AM.

  2. #32
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,457
    Quote Originally Posted by Phyxashun
    But it goes to show that I am reminded of something every day!
    Personally, I would prefer to write:
    Code:
    if (strcmp(pStudent, pOStudent) == 0)
    The ! make me think of "not equal" instead of "equal"

    Quote Originally Posted by Phyxashun
    Now I will try the std::string route, once I have that figured out i'll post it.
    Incidentally, if you are trying to figure out my example, note that you need to #include <algorithm> for std::equal(). If you are trying to write a case insensitive "less than" comparator then the same idea applies, but you might use std::lexicographical_compare() instead of std::equal().
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #33
    Registered User
    Join Date
    Dec 2008
    Posts
    65

    Thumbs up

    Quote Originally Posted by laserlight View Post
    The use of std::toupper (as opposed to the global toupper) is problematic in this case, though the exact reason eludes me at the moment. (Type mismatch? I know this has been discussed before.) Josuttis suggests just defining a function that compares characters without regard for case, and then use say, equal(), e.g.,
    Code:
    bool noCaseEqualChar(char lhs, char rhs)
    {
        return std::toupper(lhs) == std::toupper(rhs);
    }
    
    bool noCaseEqual(const std::string& lhs, const std::string& rhs)
    {
        return lhs.size() == rhs.size()
            && std::equal(lhs.begin(), lhs.end(), rhs.begin(), noCaseEqualChar);
    }
    We could use function object template classes instead and thus not have to resort to the different function names.
    These functions are absolutely perfect for use when comparing std::strings.

    Thanks laserlight!

  4. #34
    Registered User
    Join Date
    Dec 2008
    Posts
    65
    Quote Originally Posted by laserlight View Post
    Josuttis suggests just defining a function that compares characters without regard for case, and then use say, equal(), e.g.,
    What page does Josuttis talk about that. I can't find it.

    Thanks in advance!

  5. #35
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,457
    Quote Originally Posted by Phyxashun
    What page does Josuttis talk about that. I can't find it.
    In the 16th printing of The C++ Standard Library: A Tutorial and Reference, Josuttis talks about this case insensitive comparison on pages 499 to 501.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #36
    Registered User
    Join Date
    Dec 2008
    Posts
    65
    Once again, thanks.


  7. #37
    Registered User
    Join Date
    Dec 2008
    Posts
    65

    Lightbulb Useful?

    This might be useful to someone:

    Code:
    /*******************************************************************************
    *   Usage:
    *       to_lower(std::string); Converts std::string to lowercase
    *       to_upper(std::string); Converts std::string to uppercase
    *******************************************************************************/
    #include <locale>
    #include <string>
    
    // to_lower(std::string)
    template <typename Char, typename Traits>
        std::basic_string<Char, Traits> &to_lower(std::basic_string<Char, Traits>
        &str, std::locale loc = std::locale())
    {
        typedef std::basic_string<Char, Traits> string;
        typedef std::ctype<Char> char_type;
        char_type const * the_type_ptr = &std::use_facet<char_type>(loc);
        for (typename string::size_type i = 0; i <str.size(); ++i)
        {
            str[i] = the_type_ptr->tolower(str[i]);
        }
        return (str);
    }
    
    // to_upper(std::string)
    template <typename Char, typename Traits>
        std::basic_string<Char, Traits> &to_upper(std::basic_string<Char, Traits>
        &str, std::locale loc = std::locale())
    {
        typedef std::basic_string<Char, Traits> string;
        typedef std::ctype<Char> char_type;
        char_type const * the_type_ptr = &std::use_facet<char_type>(loc);
        for (typename string::size_type i = 0; i <str.size(); ++i)
        {
            str[i] = the_type_ptr->toupper(str[i]);
        }
        return (str);
    }
    Cheers!

  8. #38
    Registered User
    Join Date
    Dec 2008
    Posts
    65

    Talking

    This is what I settled on:
    Code:
    void searchRecords(std::ifstream &fin, std::string &searchString, 
                       OldStudentData &obj, long recordNum)
    {
        fin.seekg(sizeof(OldStudentData) * recordNum, std::ios::beg);
        fin.read((char *) &obj, sizeof(OldStudentData));
    
        std::string personName(obj.studentname);
    
        if(noCaseEqual(searchString, personName))
        {
            // Do some stuff in here!
        }
    }
    
    bool noCaseEqual(const std::string &lhs, const std::string &rhs)
    {
        return lhs.size() == rhs.size() 
            && std::equal(lhs.begin(), lhs.end(), rhs.begin(), noCaseEqualChar);
    }
    
    bool noCaseEqualChar(char lhs, char rhs)
    {
        return std::toupper(lhs) == std::toupper(rhs);
    }
    Thanks for all of the help! Any Ideas on refinement?

Page 3 of 3 FirstFirst 123
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Inheritance Hierarchy for a Package class
    By twickre in forum C++ Programming
    Replies: 7
    Last Post: 12-08-2007, 03:13 PM
  2. Replies: 6
    Last Post: 01-03-2007, 02:02 PM
  3. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM
  4. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  5. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21