tokenize字符串不包括零终端?

发布于 2025-02-12 05:52:22 字数 1102 浏览 3 评论 0原文

我有一个我正在解析的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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

滥情稳全场 2025-02-19 05:52:23

经典阅读错误

从流中读取的最后一个读数为“最高”,但没有超过文件末尾。因此,即使文件中没有数据,feof()是错误的。然后,随后的读取将失败。

问题是您没有检查故障的fgets()的结果(即文件结尾)。

// if there is no data left to read.
// then fgets() will return NULL and this is
// equivalent to false for a while loop and thus it will
// not enter the loop.
while (fgets(row, MAX_LINE, file)) {

    // Have successful read a row.
    );
    
    // Parse row
    ....

    // Use the variables
}

摘要:始终检查您的阅读工作是否有效。

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).

// if there is no data left to read.
// then fgets() will return NULL and this is
// equivalent to false for a while loop and thus it will
// not enter the loop.
while (fgets(row, MAX_LINE, file)) {

    // Have successful read a row.
    );
    
    // Parse row
    ....

    // Use the variables
}

Summary: Always check that your read worked.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文