如何用strtok检测空行
How to detect empty line with strtok
我正在尝试将页眉与页脚分开,用空行分隔,即使用字符串“\r\n\r\n”。
我尝试使用 strtok
和分隔符 "\r\n\r\n"
,如下面的代码片段所示。
#include <stdio.h>
#include <string.h>
int main() {
char get[1024] = "HTTP/1.1 200 OK\r\nDate: Tue, 23 Jul 2019 22:52:44 GMT\r\n"
"Server: Apache/2.4.7 (Ubuntu)\r\n"
"Last-Modified: Wed, 18 Aug 2004 23:07:08 GMT\r\n"
"ETag: \"164-3e1f5b9a57f00\"\r\n"
"Accept-Ranges: bytes\r\n"
"Content-Length: 356\r\n"
"Vary: Accept-Encoding\r\n"
"Content-Type: text/html\r\n"
"\r\n"
"<html><head><title>Temporary Page</title></head><body><center><h1>New Account Temporary Page</h1>Welcome to yet another new account at Hurricane Electric.<p><h2>Come Back Soon!</h2>The New Account Owner Will Be Putting Something Interesting Here!</center><hr>Space Provided By <a href=\"http://www.he.net\">Hurricane Electric</a></body></html>";
char *html;
html = strtok(get, "\r\n\r\n");
printf("Header:\n\n%s\n\n", html);
html = strtok(NULL, "\r\n\r\n");
printf("HTML:\n\n%s\n\n", html);
return 1;
}
我希望输出是打印出的页眉,然后是 HTML 页脚。
但输出是:
Header:
HTTP/1.1 200 OK
HTML:
Date: Tue, 23 Jul 2019 22:52:44 GMT
似乎 strtok
正在使用 "\r\n"
而不是 "\r\n\r\n"
进行分隔。我该如何解决这种奇怪的行为?
strtok
分隔符参数不是字符序列。它是分隔您的标记的可能字符的列表。
所以在分隔符字符串中传递重复的字符并没有达到预期的效果。
为了说服自己,将分隔符替换为 GMT
,您会看到 header 现在是 H
,因为 strtok
从 [=16= 标记化而来], 因为它在定界符列表中。
另一种方法(dirty/no 错误检查)是使用 strstr
定位分隔符,将零放在那里,然后打印两个字符串
#include <stdio.h>
#include <string.h>
int main ()
{
char get[1024] = "HTTP/1.1 200 OK\r\nDate: Tue, 23 Jul 2019 22:52:44 GMT\r\nServer: Apache/2.4.7 (Ubuntu)\r\nLast-Modified: Wed, 18 Aug 2004 23:07:08 GMT\r\nETag: \"164-3e1f5b9a57f00\"\r\nAccept-Ranges: bytes\r\nContent-Length: 356\r\nVary: Accept-Encoding\r\nContent-Type: text/html\r\n\r\n<html><head><title>Temporary Page</title></head><body><center><h1>New Account Temporary Page</h1>Welcome to yet another new account at Hurricane Electric.<p><h2>Come Back Soon!</h2>The New Account Owner Will Be Putting Something Interesting Here!</center><hr>Space Provided By <a href=\"http://www.he.net\">Hurricane Electric</a></body></html>";
const char *delim = "\r\n\r\n";
char *html;
html = strstr(get, delim); // should test for NULL ...
html[0] = '[=10=]';
printf("Header:\n\n%s\n\n", get);
printf("HTML:\n\n%s\n\n", html+strlen(delim));
return 1;
}
输出:
Header:
HTTP/1.1 200 OK
Date: Tue, 23 Jul 2019 22:52:44 GMT
Server: Apache/2.4.7 (Ubuntu)
Last-Modified: Wed, 18 Aug 2004 23:07:08 GMT
ETag: "164-3e1f5b9a57f00"
Accept-Ranges: bytes
Content-Length: 356
Vary: Accept-Encoding
Content-Type: text/html
HTML:
<html><head><title>Temporary Page</title></head><body><center><h1>New Account Temporary Page</h1>Welcome to yet another new account at Hurricane Electric.<p><h2>Come Back Soon!</h2>The New Account Owner Will Be Putting Something Interesting Here!</center><hr>Space Provided By <a href="http://www.he.net">Hurricane Electric</a></body></html>
我正在尝试将页眉与页脚分开,用空行分隔,即使用字符串“\r\n\r\n”。
我尝试使用 strtok
和分隔符 "\r\n\r\n"
,如下面的代码片段所示。
#include <stdio.h>
#include <string.h>
int main() {
char get[1024] = "HTTP/1.1 200 OK\r\nDate: Tue, 23 Jul 2019 22:52:44 GMT\r\n"
"Server: Apache/2.4.7 (Ubuntu)\r\n"
"Last-Modified: Wed, 18 Aug 2004 23:07:08 GMT\r\n"
"ETag: \"164-3e1f5b9a57f00\"\r\n"
"Accept-Ranges: bytes\r\n"
"Content-Length: 356\r\n"
"Vary: Accept-Encoding\r\n"
"Content-Type: text/html\r\n"
"\r\n"
"<html><head><title>Temporary Page</title></head><body><center><h1>New Account Temporary Page</h1>Welcome to yet another new account at Hurricane Electric.<p><h2>Come Back Soon!</h2>The New Account Owner Will Be Putting Something Interesting Here!</center><hr>Space Provided By <a href=\"http://www.he.net\">Hurricane Electric</a></body></html>";
char *html;
html = strtok(get, "\r\n\r\n");
printf("Header:\n\n%s\n\n", html);
html = strtok(NULL, "\r\n\r\n");
printf("HTML:\n\n%s\n\n", html);
return 1;
}
我希望输出是打印出的页眉,然后是 HTML 页脚。
但输出是:
Header:
HTTP/1.1 200 OK
HTML:
Date: Tue, 23 Jul 2019 22:52:44 GMT
似乎 strtok
正在使用 "\r\n"
而不是 "\r\n\r\n"
进行分隔。我该如何解决这种奇怪的行为?
strtok
分隔符参数不是字符序列。它是分隔您的标记的可能字符的列表。
所以在分隔符字符串中传递重复的字符并没有达到预期的效果。
为了说服自己,将分隔符替换为 GMT
,您会看到 header 现在是 H
,因为 strtok
从 [=16= 标记化而来], 因为它在定界符列表中。
另一种方法(dirty/no 错误检查)是使用 strstr
定位分隔符,将零放在那里,然后打印两个字符串
#include <stdio.h>
#include <string.h>
int main ()
{
char get[1024] = "HTTP/1.1 200 OK\r\nDate: Tue, 23 Jul 2019 22:52:44 GMT\r\nServer: Apache/2.4.7 (Ubuntu)\r\nLast-Modified: Wed, 18 Aug 2004 23:07:08 GMT\r\nETag: \"164-3e1f5b9a57f00\"\r\nAccept-Ranges: bytes\r\nContent-Length: 356\r\nVary: Accept-Encoding\r\nContent-Type: text/html\r\n\r\n<html><head><title>Temporary Page</title></head><body><center><h1>New Account Temporary Page</h1>Welcome to yet another new account at Hurricane Electric.<p><h2>Come Back Soon!</h2>The New Account Owner Will Be Putting Something Interesting Here!</center><hr>Space Provided By <a href=\"http://www.he.net\">Hurricane Electric</a></body></html>";
const char *delim = "\r\n\r\n";
char *html;
html = strstr(get, delim); // should test for NULL ...
html[0] = '[=10=]';
printf("Header:\n\n%s\n\n", get);
printf("HTML:\n\n%s\n\n", html+strlen(delim));
return 1;
}
输出:
Header:
HTTP/1.1 200 OK
Date: Tue, 23 Jul 2019 22:52:44 GMT
Server: Apache/2.4.7 (Ubuntu)
Last-Modified: Wed, 18 Aug 2004 23:07:08 GMT
ETag: "164-3e1f5b9a57f00"
Accept-Ranges: bytes
Content-Length: 356
Vary: Accept-Encoding
Content-Type: text/html
HTML:
<html><head><title>Temporary Page</title></head><body><center><h1>New Account Temporary Page</h1>Welcome to yet another new account at Hurricane Electric.<p><h2>Come Back Soon!</h2>The New Account Owner Will Be Putting Something Interesting Here!</center><hr>Space Provided By <a href="http://www.he.net">Hurricane Electric</a></body></html>