Thread: string with different strlen's

  1. #1
    Registered User
    Join Date
    May 2009
    Posts
    72

    string with different strlen's

    Hi All

    I want to compare a 'const *chars' with a *char using strcmp. If I use printf

    Code:
    printf("v1=|%s| v2=|%s|\n", props[0].name, config_key) ;              // -> v1=|hello| v2=|hello|
    printf("l1=%d l2=%d\n", strlen(props[0].name), strlen(config_key) ) ; // --> l1=5 l2=6
    they look identical, but have a different length (This is on Linux, on my Mac both are 5 and the issue does not exist)

    Both originate from a different source. v1:

    Code:
    struct propentry
    {
        ptrdiff_t offset;
        const char *name;
    };
    #define PE(a) { offsetof(struct CONFIG, a), #a }
    struct propentry props[] =
    {
        PE(hello)
    } ;
    and v2:
    Code:
    FILE *fp = fopen( filename, "r" ) ;
    char line [ 128 ];
    while(fgets(line, sizeof(line), fp) != NULL)
    {
      ... determine pos ...
      char *config_key = malloc( pos * sizeof(*config_key) + 1 ) ; // add 1 for 'null termination'
      substring( config_key, line, 0, pos ) ;
      ....
    }
    ....
    void substring(char* dest, const char* src, int start, int stop)
    {
       if ( stop == -1 )
         stop = strlen(src) ;
       strncpy( dest, src + start, stop ) ; // doesn't copy '\0' (null termination)
       dest[strlen(dest) ] = '\0' ; // null termination
    }
    and I compare them like
    Code:
    if ( strcmp( props[0].name, config_key) == 0 ) { ... }
    I would say I do something wrong with the 'null termination', but I can't figure it out. Any suggestions ?

    Cheers
    Luca

  2. #2
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    What do you see in the debugger?
    There's probably an invisible character in one of them.
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    You need to get a better look at the string.
    Code:
    #include <stdio.h>
    #include <string.h>
    
    void showstring (char *s) {
        int i, j;
    	for (i=0;i<strlen(s);i++) {
    		if (i && !(i%10)) {
    			printf("| ");
    			for (j=i-10;j<i;j++) printf("%c", s[j]);
    			printf("\n");
    		}
    		printf("%3d ",s[i]);
    	}
    	if (i%10) {
    		printf("| ");
    		for (j=i-i%10+1;j<i;j++) printf("%c", s[j]);
    		printf("\n");
    	}
    }
    
    int main() {
    	char str[] = "I would say I do something wrong with the 'null termination'," 
                    " but I can't figure it out. Any suggestions ?";
    
    	showstring(str);
    
        return 0;
    }
    This will produce output like:

    Code:
     73  32 119 111 117 108 100  32 115  97 | I would sa
    121  32  73  32 100 111  32 115 111 109 | y I do som
    101 116 104 105 110 103  32 119 114 111 | ething wro
    110 103  32 119 105 116 104  32 116 104 | ng with th
    101  32  39 110 117 108 108  32 116 101 | e 'null te
    114 109 105 110  97 116 105 111 110  39 | rmination'
     44  32  98 117 116  32  73  32  99  97 | , but I ca
    110  39 116  32 102 105 103 117 114 101 | n't figure
     32 105 116  32 111 117 116  46  32  65 |  it out. A
    110 121  32 115 117 103 103 101 115 116 | ny suggest
    105 111 110 115  32  63 | ons ?
    The text on the right will (probably) look the same in both cases; you need to find the oddball in the values on the left.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  4. #4
    Registered User
    Join Date
    May 2009
    Posts
    72
    Here is the output:

    Code:
     window_shift --> 12 en 13
    119 105 110 100 111 119  95 115 104 105 102 116   7 | window_shift
    119 105 110 100 111 119  95 115 104 105 102 116 | window_shift
    What does the 7 do ?

    So I found out where it goes wrong in my code:

    Code:
    // line will look like: window_shift=10
    while(fgets(line, sizeof(line), fp) != NULL)
       {
           int len = strlen( line ) - 1;
           if ( len > 0 )
            {
               if(line[len] == '\n')  
                 line[len] = 0;        // <---- PROBLEM
               
               int pos = findchar(line, "=") ;
    
               char *config_key = malloc( pos * sizeof(*config_key) + 1 ) ; // add 1 for 'null termination'
               substring( config_key, line, 0, pos ) ;
               ........
        }
    ......
    void substring(char* dest, const char* src, int start, int stop)
     {
         strncpy( dest, src + start, stop ) ; // doesn't copy '\0' (null termination)
         dest[strlen(dest) ] = '\0' ; // null termination  <--- problem
    }
    So, the line
    Code:
    line[len] = 0;
    is the problem. If I change it to
    Code:
    line[len] = '\0';
    it works
    Are they different ? And why does it work on my mac!

    UPDATE: not sure what I just did, but line[len] = '\0' didn't fix it. If I remove the line, that fixes it!!!
    Last edited by jeanluca; 03-26-2010 at 01:23 AM.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Err... actually, there should be no difference. '\0' is a character literal with the value of 0. Since in C character literals are of type int, it really should be the same as just writing 0 (except that 0 would not indicate to the reader that a character is involved here, unlike '\0').
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Pure guess: did you write the file? If it was written on a different system, with different line endings, you may be reading "\r\n" instead of "\n" at the end of the line. So maybe peel off two characters instead of one?

    (On the other hand, \r is not ASCII 7, which is ... that's beep, isn't it?)

  7. #7
    Registered User
    Join Date
    May 2009
    Posts
    72
    Thnx for all the help!
    I'm using gdb and valgrind now (which I didn't before) and I'm making progress!!
    For example, I noticed that the function 'substring' wasn't correct at all
    But I think there's more!

    cheers

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. OOP Question DB Access Wrapper Classes
    By digioz in forum C# Programming
    Replies: 2
    Last Post: 09-07-2008, 04:30 PM
  2. C++ ini file reader problems
    By guitarist809 in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2008, 06:02 AM
  3. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  4. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 03:23 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM