简体   繁体   English

fscanf读取访问段错误

[英]fscanf read access segfault

I a successfully open a file that contains initial key-value pairs. 我成功打开了一个包含初始键值对的文件。 The code looks like this: 代码如下:

char key_tmp[30];
int value_tmp, status_index;

FILE *ZMU_Init_fd = NULL;
ZMU_Init_fd = fopen("zmu/zmu.cfg", "r");

if(ZMU_Init_fd ==  NULL)
{
    printf("Could not open file");
    strerror(errno);
}

printf("key tmp is %s, value tmp is %d\n", key_tmp, value_tmp);
fscanf(ZMU_Init_fd, "%s %s", key_tmp, (char *)value_tmp);
printf("key tmp is %s, value tmp is %d\n", key_tmp, value_tmp);

When I run i get: 当我跑步时,我得到:

key tmp is y? value tmp is 1

then it segfaults. 然后它出现段错误。 When I change access to "w" the code sets zmu.cfg file size to 0 and i get: 当我将访问权限更改为“ w”时,代码将zmu.cfg文件大小设置为0,我得到:

key tmp is y? value tmp is 1
key tmp is y? value tmp is 1

no segfault... Interestingly enough, once the file is 0 bytes I can change the access back to "r" it no longer segfaults and i get: 没有段错误...有趣的是,一旦文件为0字节,我就可以将访问权限改回“ r”,它不再出现段错误,我得到:

key tmp is y? value tmp is 1
key tmp is y? value tmp is 1

I believe this has something to do with the way fscanf works. 我相信这与fscanf的工作方式有关。 Does fscanf delete the line from the file when it scans? fscanf扫描时是否从文件中删除该行? All I am trying to do is get a key-value pair from a text file. 我要做的就是从文本文件中获取键值对。 I dont think i can use fread because the keys dont always have the same size. 我不认为我可以使用fread,因为密钥并不总是具有相同的大小。 I do NOT want the file modified, any thoughts? 我不想修改文件,有什么想法吗?

zmu.cfg looks like this: zmu.cfg看起来像这样:

MessageNum= message50
MsgInitOne= 0x01
MsgInitTwo= 0x04
MsgInitThree= 0x01

MessageNum= message50
VarOne= 0x02
VarTwo= 0x01

MessageNum= message50
MsgValOne= 0x01
MsgValTwo= 0x04
MsgValThree= 0x02

You haven't initialized key_tmp before using it here. 您尚未在此处使用key_tmp进行初始化。

printf("key tmp is %s, value tmp is %d\n", key_tmp, value_tmp);

You are passing the wrong argument to fscanf . 您将错误的参数传递给fscanf Casting value_tmp to char* does not enable it to hold a string. value_tmpchar*不能使其容纳字符串。

fscanf(ZMU_Init_fd, "%s %s", key_tmp, (char *)value_tmp);

If the file contains an integer, use: 如果文件包含整数,请使用:

fscanf(ZMU_Init_fd, "%s %d", key_tmp, &value_tmp);

If the file contains a string, make sure that value_tmp is a string large enough to hold the data. 如果文件包含字符串,请确保value_tmp是足以容纳数据的字符串。

char value_temp[1000];

and then, use: 然后,使用:

fscanf(ZMU_Init_fd, "%s %s", key_tmp, value_tmp);

You experience undefined behavior on your first call to: 首次拨打以下电话时,您会遇到不确定的行为

printf("key tmp is %s, value tmp is %d\n", key_tmp, value_tmp);

Neither key_tmp or value_tmp are initialized to any value. key_tmpvalue_tmp都不会初始化为任何值。 After you attempt to access the uninitialized values, all bets are off. 尝试访问未初始化的值后,所有投注均关闭。 Your code could give you some output, or it could segfault -- you are experiencing undefined behavior -- anything could happen. 您的代码可能会给您一些输出,也可能会出现段错误-您正在遇到未定义的行为 -可能会发生任何事情。

Further, if you are reading an int in value_tmp , your fscanf format string should be: 此外,如果您正在读取value_tmpintvalue_tmp fscanf格式字符串应为:

fscanf(ZMU_Init_fd, "%s %d", key_tmp, &value_tmp);

Yay! 好极了! I changed the last lines to this: 我将最后几行更改为:

fscanf(ZMU_Init_fd, "%s %s", key_tmp, key_tmp);
printf("key tmp is %s, value tmp is %d\n", key_tmp, value_tmp);
fscanf(ZMU_Init_fd, "%s %x", key_tmp, &key_tmp);
printf("key tmp is %s, value tmp is %d\n", key_tmp, value_tmp);

and I get: 我得到:

key tmp is message50 value tmp is 8388608
key tmp is MsgInitOne value tmp is 1

Thanks for looking at my code, didn't think i needed to pass the address of key_tmp to fscanf for %i since I did not have to for %s. 感谢您查看我的代码,我认为我不需要将key_tmp的地址传递给fscanf以获得%i,因为我不必传递%s。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM