Reputation: 75
I have this little problem when trying to tokenize a string from a http request directed at my "home made" Http server.
Basicly I am using these lines of code to tokneize.
token = strtok(bufptr, "\n");
while(token != NULL){
printf("%s \n", token);
token = strtok(NULL, "\n");
}
The problem is that for every token, the first character is removed from the tokenized string. How can I solve this?
I have tried copying the string, I have tried using strstr, but I have not yet succeded. I bet there is something pretty easy I am doing wrong.
Best regards.
Upvotes: 1
Views: 1598
Reputation: 75
For anyone else that is having this problem. The HTTP protocol uses the \r\n delimiters after the header lines, and a CRLN for seperating the body from the header. CRLN is \r\n\r\n as I understood it.
From my searching around the web I did not find alot easy readable information about the protocol. Of course, you do have the RFCs.
Thanks for the help everybody. Btw, I could never figure out how to accept an answer.
Upvotes: 0
Reputation: 75
I got it working by using the \r\n as delimiter instead of only \n I did not think UNIX sent \r\n for newline, but seems that it does? Anyways, thanks.
Upvotes: 0
Reputation: 153457
You have a '\r'
in the string. (@Alessandro Suglia)
printf("%sx\n", "abc\r"); // x substituted for space
Prints:
xbc
The '\r'
(return) move the print position to the beginning of the line. Then 'x'
overwrites 'a'
. This appears as the OP observed "the first character is removed from the tokenized string".
Upvotes: 3
Reputation: 7118
This is working just fine:
char bufptr[] = "aaaa\nbbbb\ncccc\nddd";
char *token;
token = strtok(bufptr, "\n");
while(token != NULL){
printf("%s \n", token);
token = strtok(NULL, "\n");
}
output is:
aaaa
bbbb
cccc
ddd
Upvotes: 0