Thread: strcmp() not returning 0 when its suppose to

  1. #16
    Registered User
    Join Date
    Oct 2008
    Posts
    103
    well i am trying to store it into a buffer, however I am finding troubles getting the buffer to work:

    here's a sample of what I have going on now:


    Code:
    char msgBuf ="NULL";
    
    	while( (num = read(0,message,sizeof(message))) > 0){
    		
    		
    		
    
    		if(strcmp(message, "exit\n") == 0){
    			printf("Exiting . . .\n");
      			/* Close this connection. */
    			pthread_join(root, &temp);
      			close(sock);
    			exit(0);
    		}
    		
    					
    		//printf("EOM CHECK WITH %s, %d \n", message, strcmp(message,"EOM\n"));
    
    		
    		if(strcmp(message, "SEND\n") == 0){
    			printf("SEND DOES = 0\n");
    			if(send(sock,msgBuf,1024,0) < 0){
    			pdie("Writing on stream socket");
    			}
    		}else{
    			strcat(msgBuf,message);
    			printf("Sizeof msgBuf %d\n", sizeof(msgBuf));
    			//strcat(msgBuf, message);
    			printf("msgBuf:\n %s", msgBuf);		
    		}
    If "SEND" is not the first input then it never works, thus always "strcat" the string. And then random things seem to start showing up:


    Sizeof msgBuf 5
    msgBuf:
    Nullhi there
    what going on
    do you smell anything
    SEND
    ou mell anything
    I have no idea where "ou smell anything" is coming from

  2. #17
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Well for starters, read doesn't guarantee you're actually receiving a string. Just a bunch of bytes. So, you shouldn't really be assuming it's a string.
    Code:
    message[num] = '\0';

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #18
    Registered User
    Join Date
    Oct 2008
    Posts
    103
    hm i see.

    Also i was wondering:

    since (in my case) read() is storing what it reads into message[] do i flush message[] by simply

    Code:
    message[0] = 0;
    I'm beginning to see the problem that message[] is getting written in to way to much

    hi there //input from command line
    Message len 9
    total = 9
    go //input from command line
    Message len 9
    total = 18
    how would i reset message[]?

  4. #19
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You don't need to reset the message. You need to be making your strings nul-terminated. I suppose if you don't like that, and want to do it the more expensive way, you could call memset on it. But again, you really just need to treat your strings right.


    Quzah.
    Hope is the first step on the road to disappointment.

  5. #20
    Registered User
    Join Date
    Oct 2008
    Posts
    103
    Quote Originally Posted by quzah View Post
    You don't need to reset the message. You need to be making your strings nul-terminated. I suppose if you don't like that, and want to do it the more expensive way, you could call memset on it. But again, you really just need to treat your strings right.


    Quzah.
    Sorry, I need clarification:

    to nul-terminate my strings i would add a '\0' at the end of message[] at every read. But how would that make a difference?

    Code:
    while( (num = read(0,message,sizeof(message))) > 0)
    
               message[strlen(message)++] = '\0';

  6. #21
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by TaiL View Post
    Sorry, I need clarification:

    to nul-terminate my strings i would add a '\0' at the end of message[] at every read. But how would that make a difference?

    Code:
    while( (num = read(0,message,sizeof(message))) > 0)
    
               message[strlen(message)++] = '\0';
    Because in C, char's without an end of string char to elevate them, are just a collection of char's - and not a string.

    You can't expect standard C string functions to work on them.

    bunch of char's: ABCDEFGH

    string: ABCDEFGH'\0'

    Which many functions in C will make for you: fgets(), for one.

  7. #22
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by TaiL View Post
    message[strlen(message)++] = '\0';
    No. I already showed you the right way to do it. You can't use string functions on things that aren't strings. That means you can't use strlen on an array of characters to turn it into a string.


    Quzah.
    Hope is the first step on the road to disappointment.

  8. #23
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Tail, strlen() makes no sense, when used on something that is *NOT* a string.

    Doesn't compute.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Fucntion returns -1, Why?
    By Taper in forum C Programming
    Replies: 16
    Last Post: 12-08-2008, 06:30 PM
  2. help with switch statement
    By agentsmith in forum C Programming
    Replies: 11
    Last Post: 08-26-2008, 04:02 PM
  3. strcmp returning 1...
    By Axel in forum C Programming
    Replies: 12
    Last Post: 09-08-2006, 07:48 PM
  4. strcmp
    By kryonik in forum C Programming
    Replies: 9
    Last Post: 10-11-2005, 11:04 AM
  5. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM