Parsing buffer with strchr

This is a discussion on Parsing buffer with strchr within the C Programming forums, part of the General Programming Boards category; Hi all, I am trying to parse the following buffer, each are seperated by a '|' char, Code: char string[] ...

  1. #1
    Registered User
    Join Date
    Dec 2004
    Location
    The Netherlands
    Posts
    91

    Parsing buffer with strchr

    Hi all,
    I am trying to parse the following buffer, each are seperated by a '|' char,
    Code:
    char string[] = "word 1 |word2  | word3| word4|word5";
    I got the following, its a bit messy, but I hope someone can point me to the right direction.

    Code:
    int main(void)
    {
    	char word[128];
    	char *p, prefix;
    	int i;
    	char string[] = "word 1 |word2  | word3| word4|word5";
    
    	prefix = '|';
    	
    	p = strchr(string, prefix);
    
    	if(p == NULL) {
    		printf("No %c found.\n", prefix);
    	}
    	else {
    		i = p-string;
    		//printf("Found %c at position: %d\n", prefix, i+1);
    		strncpy(word, &string[0], i);
    		word[i] = '\0';
    		printf("Word: [%s]\n", word);
    
    		do {
    			p=strchr(p+1, prefix);
    			strncpy(word, &string[i], (p-string)-i);
    			word[p-string] = '\0';
    
    			printf("Word: [%s]\n", word);
    
    			i = p-string;
    			//printf("Found %c at position: %d\n", prefix, i+1);
    		} while(p != NULL);
    	}
    
    	return 0;
    }
    Output:
    Code:
    Word: [word 1 ]
    Word: [|word2  ╠╠╠╠╠╠╠]
    Word: [| word3 ╠╠╠╠╠╠╠]
    Word: [| word4 ╠╠╠╠╠╠╠]
    Which should be:
    Code:
    Word: [word 1]
    Word: [word2]
    Word: [word3]
    Word: [word4]
    Word: [word5]
    Any help and info are much appreciated.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    p1 = string;
    while( (p2 = strchr( p1, character )) != NULL )
    {
        *p2 = '\0';
        strcpy( word, p1 );
        p1++ = p2++;
    }

    That looks about right. Whenever you see a bunch of screwed up characters in a string you're trying to print, it means you've missed a null character some place.


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

  3. #3
    Registered User
    Join Date
    Dec 2004
    Location
    The Netherlands
    Posts
    91
    Thanks for the reply, but these colors are surely hard to read .

    I have one more question thats bothering me, the loop stops before extracting the last word out, since there are 4 '|'s and 5 words, loop misses the last word. Is there a good way to avoid this, I was thinking of doing the same step once again after the loop.

  4. #4
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,638
    Because the last word isn't suffixed with a vertical bar, strchr returns failure. The last "word" should be stored in the original buffer, or pointers to it. You simply have to take this into account and strcpy one extra time.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 10-29-2006, 04:04 AM
  2. writing a pack-style function, any advices?
    By isaac_s in forum C Programming
    Replies: 10
    Last Post: 07-08-2006, 08:09 PM
  3. buffer contents swapping
    By daluu in forum C++ Programming
    Replies: 7
    Last Post: 10-14-2004, 02:34 PM
  4. Tetris Questions
    By KneeGrow in forum Game Programming
    Replies: 19
    Last Post: 10-28-2003, 10:12 PM
  5. Console Screen Buffer
    By GaPe in forum Windows Programming
    Replies: 0
    Last Post: 02-06-2003, 04:15 AM

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