Menu

gcc 2.95: strange results with strings

2003-01-19
2012-09-26
  • Patrick Ogay

    Patrick Ogay - 2003-01-19

    I never trusted string handling in C/C++, besides I'm coming from PL/I :-)
    But now I seem to have a fundamental problem:
    gcc version 2.95.3-6 / win2k

    Until now I was convinced that:
    string a("blabla");
    const char *ptr = a.data(); 

    I never realized problems with that until yesterday.
    I can not say it doesn't work, but sometimes the results are undefinded, seems to be a initialization problem.

    May be, you guys, can give my a few hints.

    Patrick

    //  Routine for internet, Robert told me, never having problems with it
    //
    std::string GetIniSetting(INIFile &theINI, const char *section, const char *key, const char *defaultval)
    {
        std::string result(defaultval);
            if (key==0) return result;
        INIFile::iterator iSection = theINI.find(std::string(section));
        if(iSection != theINI.end())
        {
            INISection::iterator apair = iSection->second.find(std::string(key));
            if(apair != iSection->second.end()){
                result = apair->second;
                _log("+1 GetIniSet: \\%s\\\n", apair->second.data());  01]  // pog test
                    }
        }
    ==>    // ---- pog trying to correct the strings...
        string mod_string;
        mod_string ="                                                         "; // some tests
        mod_string ="";
        int pos=result.find(";");
            mod_string = result.substr(0,pos); // even tried + "\0";
        _log("+2 modstring \\%s\\ %i\n", mod_string.data(),mod_string.length());
            // ---- pog end
        return result;
    }

    Test-Output:
    +1 GetIniSet: \yes;\                 ok
    +2 modstring \yes\ 3                 correction ok
    +1 GetIniSet: \scan;\
    +2 modstring \scan\ 4
    +1 GetIniSet: \[a,A,p,P,g,G][P][0-9]{5};\
    +2 modstring \[a,A,p,P,g,G][P][0-9]{5}\ 24
    +1 GetIniSet: \[.]doc;\
    +2 modstring \[.]doc\ 6
    +1 GetIniSet: \network;\              string ok
    +2 modstring \networkdfasdf\ 7        correction makes string even wrong! length of string ok.
    +1 GetIniSet: \\copy_place; \
    +2 modstring \\copy_place\ 11
    +1 GetIniSet: \log01.txt;\
    +2 modstring \log01.txtasdf\ 9
    +1 GetIniSet: \2002;\
    +2 modstring \2002\ 4
    +1 GetIniSet: \02;                                                      \
    +2 modstring \02nth\ 2
    +1 GetIniSet: \15;\
    +2 modstring \15y\ 2
    +1 GetIniSet: \02;v\
    +2 modstring \02lv\ 2
    +1 GetIniSet: \5;lv\             01]  cases I want to correct  
    +2 modstring \5_lv\ 1                 string always wrong but lenght ok.

    // logging routine
    void _log(const char *str, ...){
      va_list         args;
      va_start    (   args,   str);

      static FILE *outstream;
      static int open = 0;
      if (open==0){
        outstream = fopen("log01.txt", "w"); open = 1;
      }
     
      vfprintf(outstream,str,args);
      fflush(outstream);
      va_end  (args);
    }

     
    • Anonymous

      Anonymous - 2003-01-19

      You should use
      const char *ptr = a.c_str();

      This is guaranteed to return a pointer to a C style string (NUL terminated character array).

       
    • Patrick Ogay

      Patrick Ogay - 2003-01-19

      the example works with c_str() !!!!

      I think all problems I had, (particularly with the template)  came form the wrong conversion with a.data().

      thank you very much!

      Patrick

       

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.