tokenize字符串不包括零终端?
我有一个我正在解析的CSV文件,这些文件看起来像是这样的条目:
GET,/mic-check/one.php/two/,NULL,0
POST,/mic-check/one.php,?wc-ajax=add_to_cart,0
...
GET,/mic-check/one.php/checkout/,NULL,0
GET,/mic-check/one.php/my-account/,NULL,0\0
我逐行遍历此文件,然后将每个文件分开,将值放入其各自的变量中。它可以正常运行,直到绝对的最后一行保持终止字符的“%”。示例:
Method: GET
URL: /mic-check/one.php/my-account/
Query: NULL
Servers: 0%
我想做的是在解析过程中不包括这个角色。这是我的代码下面:
int main()
{
FILE *file;
char row[MAX_LINE];
char method[MAX_COLUMN];
char url[MAX_COLUMN];
char query[MAX_COLUMN];
char servers[MAX_COLUMN];
char *tkn;
file = fopen("whitelist.csv", "r");
while(feof(file) != true) {
// Receive row
fgets(row, MAX_LINE, file);
// Parse row
tkn = strtok(row, ",");
strcpy(method, tkn);
tkn = strtok(NULL, ",");
strcpy(url, tkn);
tkn = strtok(NULL, ",");
strcpy(query, tkn);
tkn = strtok(NULL, ",");
strcpy(servers, tkn);
// Use the variables
}
return 0;
}
I have a csv file that I am parsing with entries that look like:
GET,/mic-check/one.php/two/,NULL,0
POST,/mic-check/one.php,?wc-ajax=add_to_cart,0
...
GET,/mic-check/one.php/checkout/,NULL,0
GET,/mic-check/one.php/my-account/,NULL,0\0
I go through this file row by row and split each, putting the values into their respective variables. It works okay up until the absolute last line where it keeps the terminating character '%'. Example:
Method: GET
URL: /mic-check/one.php/my-account/
Query: NULL
Servers: 0%
What I would like to do, is not include this character during the parsing. Here is my code below:
int main()
{
FILE *file;
char row[MAX_LINE];
char method[MAX_COLUMN];
char url[MAX_COLUMN];
char query[MAX_COLUMN];
char servers[MAX_COLUMN];
char *tkn;
file = fopen("whitelist.csv", "r");
while(feof(file) != true) {
// Receive row
fgets(row, MAX_LINE, file);
// Parse row
tkn = strtok(row, ",");
strcpy(method, tkn);
tkn = strtok(NULL, ",");
strcpy(url, tkn);
tkn = strtok(NULL, ",");
strcpy(query, tkn);
tkn = strtok(NULL, ",");
strcpy(servers, tkn);
// Use the variables
}
return 0;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
经典阅读错误。
从流中读取的最后一个读数为“最高”,但没有超过文件末尾。因此,即使文件中没有数据,
feof()
是错误的。然后,随后的读取将失败。问题是您没有检查故障的
fgets()
的结果(即文件结尾)。摘要:始终检查您的阅读工作是否有效。
Classic reading mistake.
The last read from a stream reads "upto" but not past the end of file. Thus
feof()
is false even though there is no data in the file. A subsequent read will then fail.The problem is you do not check the result of
fgets()
for a failure (i.e. end of file).Summary: Always check that your read worked.