以下 strtok() 用法有什么问题?
我正在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
正如 paulsm4 所说,您需要检查
strtok()
返回值。以下代码有效并包括字符串的创建:分隔符设置为
", \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:Delimiter set is
", \n"
asfgets()
will read the newline character intobuf
.您需要检查 NULL 返回:
You need to check for NULL return: