以下 strtok() 用法有什么问题?

发布于 2024-12-28 16:29:33 字数 1648 浏览 1 评论 0原文

我正在使用 vc2010,我试图将文件的内容读取到结构中,如下所示,它给了我运行时错误。

char buf[100];
char *token = NULL; 
while( fgets (buf , 100 , rd) != NULL )
{
    token = strtok( buf,", ");
    test_st.fp.chunk_offset = atol(token); 
    printf("\n %llu ", test_st.fp.chunk_offset); 

    //OPTION 1: if i do this there will be no runtime error but the same
    // value as the first token will be assigned to chunk_length
    token = strtok(buf, ",");
    //OPTION 2: this line gives error in the second while loop iteration
    token=strtok(NULL,",");
    test_st.fp.chunk_length = atol(token);
    printf("%llu ",  test_st.fp.chunk_length);  
    token = strtok(NULL, " ");
    ....
}

我的另一个问题是我如何使用 strtok() 或任何(如果有的话)将一个很长的字符串(如以下文件内容中第二个逗号后面的字符串)分配给结构数据成员(在本例中为fp.fing_print)?

这是我试图读取

0,4096,2ed692b40e335f7c20b71c5ed0486634
4096,3028,da40bf20c8ff189087b8bd7e8580118a
7124,2177,e6dfaee81e96095d302c82f9fd73dc55
9301,1128,76201eadff3c89a381a314ed311f75ff

结构定义的文件的第一部分,我试图将其读入是:

typedef struct fpinfo
{
  unsigned long chunk_offset;
  unsigned long chunk_length;
  unsigned char fing_print[32];
}fpinfo;

typedef struct Hash_Entry {
   struct Hash_Entry *next;  /* Link entries within same bucket. */
   unsigned namehash;        /* hash value of key */
   struct fpinfo fp;
} Hash_Entry;

编辑:
是的,你是对的,我必须使用第二个选项。
我发现这个有问题。该文件每行之间都有空行,当到达这些空行时会给出错误消息。

但我陷入了另一个问题,我必须将每行的最后一部分读入结构的数组成员(fp.fing_print)。

谢谢大家的帮助!!!

编辑: fing_print[32] 数组应该保存 32 个字符,它们是 md5 哈希函数的结果。我不确定是否必须将其设为空终止字符串。如果你们能给我一些建议,我将不胜感激。

I am using vc2010 and I am trying to read contents of a file into a struct as follows and it gives me run time error.

char buf[100];
char *token = NULL; 
while( fgets (buf , 100 , rd) != NULL )
{
    token = strtok( buf,", ");
    test_st.fp.chunk_offset = atol(token); 
    printf("\n %llu ", test_st.fp.chunk_offset); 

    //OPTION 1: if i do this there will be no runtime error but the same
    // value as the first token will be assigned to chunk_length
    token = strtok(buf, ",");
    //OPTION 2: this line gives error in the second while loop iteration
    token=strtok(NULL,",");
    test_st.fp.chunk_length = atol(token);
    printf("%llu ",  test_st.fp.chunk_length);  
    token = strtok(NULL, " ");
    ....
}

my other problem is how can i use strtok() or any- if there is one- to assign a very long character string(like the one after the second coma in the following file content) into a structure data member(in this case into fp.fing_print)?

Here is the first part of the file i am trying to read

0,4096,2ed692b40e335f7c20b71c5ed0486634
4096,3028,da40bf20c8ff189087b8bd7e8580118a
7124,2177,e6dfaee81e96095d302c82f9fd73dc55
9301,1128,76201eadff3c89a381a314ed311f75ff

the structure definition i am trying to read this into is:

typedef struct fpinfo
{
  unsigned long chunk_offset;
  unsigned long chunk_length;
  unsigned char fing_print[32];
}fpinfo;

typedef struct Hash_Entry {
   struct Hash_Entry *next;  /* Link entries within same bucket. */
   unsigned namehash;        /* hash value of key */
   struct fpinfo fp;
} Hash_Entry;

EDIT:
Yes you are right I have to use the second option.
I HAVE FOUND THE PROBLEM WITH THIS ONE. The file have empty lines between every line and it gives error message when it gets to these empty lines.

BUT I AM STUCK in the other problem where I have to read the last part of each line into the array member(fp.fing_print) of the structure.

Thank you everybody for your help!!!

EDIT:
the fing_print[32] array is supposed to hold 32 characters which are results of an md5 hash function. I am not sure if i have to make it a null terminated string or not. if you guys could give me a tip about it I will be more than grateful.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

葬﹪忆之殇 2025-01-04 16:29:33

正如 paulsm4 所说,您需要检查 strtok() 返回值。以下代码有效并包括字符串的创建:

while( fgets (buf , 100 , rd) != NULL )
{
    token = strtok(buf,", \n");
    if (0 != token)
    {
        test_st.fp.chunk_offset = atol(token); 
        printf("\tchunk_offset=%lu\n", test_st.fp.chunk_offset); 

        token = strtok(0, ", \n");
    }

    if (0 != token)
    {
        test_st.fp.chunk_length = atol(token);
        printf("\tchunk_length=%lu\n", test_st.fp.chunk_length);  
        token = strtok(0, ", \n");
    }

    if (0 != token)
    {
        /* EDIT: fing_print datatype changed. */
        memset(test_st.fp.fing_print, 0, sizeof(test_st.fp.fing_print));
        strncpy(test_st.fp.fing_print,
                token,
                sizeof(test_st.fp.fing_print) - 1);

        /* test_st.fp.fing_print = _strdup(token); */

        printf("\tfing_print=[%s]\n", test_st.fp.fing_print);
    }

    printf("\n");
}

分隔符设置为 ", \n" 因为 fgets() 会将换行符读入 buf.

As stated by paulsm4 you need to check strtok() return value. The following code works and includes the creation of the string:

while( fgets (buf , 100 , rd) != NULL )
{
    token = strtok(buf,", \n");
    if (0 != token)
    {
        test_st.fp.chunk_offset = atol(token); 
        printf("\tchunk_offset=%lu\n", test_st.fp.chunk_offset); 

        token = strtok(0, ", \n");
    }

    if (0 != token)
    {
        test_st.fp.chunk_length = atol(token);
        printf("\tchunk_length=%lu\n", test_st.fp.chunk_length);  
        token = strtok(0, ", \n");
    }

    if (0 != token)
    {
        /* EDIT: fing_print datatype changed. */
        memset(test_st.fp.fing_print, 0, sizeof(test_st.fp.fing_print));
        strncpy(test_st.fp.fing_print,
                token,
                sizeof(test_st.fp.fing_print) - 1);

        /* test_st.fp.fing_print = _strdup(token); */

        printf("\tfing_print=[%s]\n", test_st.fp.fing_print);
    }

    printf("\n");
}

Delimiter set is ", \n" as fgets() will read the newline character into buf.

我要还你自由 2025-01-04 16:29:33

您需要检查 NULL 返回:

  if ((token = strtok(buf, ",")) {
    // must have found a comma
  }
  if (token && (token = strtok(NULL,"'")) {
    // Two in a row: I found a comma and then I found an apostrophe
  }
  ...

You need to check for NULL return:

  if ((token = strtok(buf, ",")) {
    // must have found a comma
  }
  if (token && (token = strtok(NULL,"'")) {
    // Two in a row: I found a comma and then I found an apostrophe
  }
  ...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文