How to extract chars from char array
I am trying to extract char arrays from a buffer but stops working after it extracts the first char array.
char *msg = "1~Message~ILOVEYOU\r\n2~Message~Doyouloveme?\r\n3~Message~OfcourseIdo!Not!\r\n";
char tempbuffer[1024];
char *tbuf;
tbuf = &tempbuffer[0];
/* Start parsing */
while (*msg != '\0') {
while(*msg != '\n') {
while (*msg != '\r') {
*tbuf = *msg;
msg++;
tbuf++;
} /* closing '\r' */
msg++;
tbuf++;
} /* closing '\n' */
*tbuf = '\0';
/* Printout buffer for debugging purposes */
printf("x %s\n", temp开发者_运维百科buffer);
/* Clear tempbuffer before starting to parse the buffer again */
memset(tempbuffer, 0, sizeof((char) 1024));
} /* closing '\0' */
return 0;
}
The printf shows 1~Message~ILOVEYOU and it stops working. I am expecting the following output
1~Message~ILOVEYOU
2~Message~Doyouloveme?
3~Message~OfcourseIdo!NOT!
Any ideas?
You need to reset tbuf to point back to the start of tempbuffer before you start the 2nd and subsequent loops.
Some problems I could see:
1: Incorrect argument to memset
.
Change
memset(tempbuffer, 0, sizeof((char) 1024));
to
memset(tempbuffer, 0, sizeof(tempbuffer));
Actually there is no real need for memset
here.
2: Not incrementing the pointer msg
when you encounter a \n
.
Add
msg++;
before/after
*tbuf = '\0';
3: Reset tbuf
to the start of the array at the start of each iteration
Add
tbuf = &tempbuffer[0];
Inside the first while
loop.
4: *tbuf = '\0';
should be *(tbuf-1) = '\0';
as you've already incremented tbuf
at this point.
See it work
Use a debugger and step through your programm. Or do it manually with pen and paper. What happens when you encounter the first '\n'? What happens, afterwards? You are stuck on that '\n' because you never move the read-pointer after printing the parsed message.
*tbuf='\0' should be *(tbuf-1) because you add the terminating zero after one unnecessary simbol. And why you need to call memset. You can write over the old data in the buffer.
Here's the fixed code...
Problems:
- sizeof was wrong
- You did not increment msg after each '\n'
- You did not set tbuf back to the beginning of tempbuffer after each '\n'
- While it's not always reported as an error, you were missing some include files, which can cause problems
#include <string.h> #include <stdio.h> char *msg = "1~Message~ILOVEYOU\r\n2~Message~Doyouloveme?\r\n3~Message~OfcourseIdo!Not!\r\n"; char tempbuffer[1024]; char *tbuf = &tempbuffer[0]; int main( int argc, char *argv[] ) { argc = argc; argv[0] = argv[0]; /* Start parsing */ while( *msg != '\0' ) { while( *msg != '\n' ) { while( *msg != '\r' ) { *tbuf = *msg; msg++; tbuf++; } msg++; tbuf++; } *tbuf = '\0'; /* Printout buffer for debugging purposes */ printf("x %s\n", tempbuffer); /* Clear tempbuffer before starting to parse the buffer again */ memset(tempbuffer, 0, sizeof(tempbuffer)); tbuf = &tempbuffer[0]; msg++; } return 0; }
i suggest u look at the string function strtok..if u want a simpler code than the one presented let me know
精彩评论