我用C语言编写了一些小东西,然后遇到了一个问题:
我正在尝试解析一个文件,其中包含以下格式的行:
name: description text: 42
name 2: description text2: 75
因此,当我阅读每一行时,我将它们输入到scanf中,如下所示:
uint8_t buffer [512]={0};
uint8_t name[32]={0};
uint8_t desc[32]={0};
uint8_t chances;
[...]
scanres = sscanf( buffer, "%31[0-9a-zA-Z ]:%31[0-9a-zA-Z ]:%d", name, desc, &num );
但是即使scanres为“ 3”(正确的行为); “ desc”仍显示为“”(空字符串)
有人可以帮我吗?我在做什么错了?
[编辑] 这是情况下的代码:
#define TINY_STRING_SIZE 32
// [...]
FILE *conffp;
uint8_t i=0;
uint8_t scanres = 0;
uint8_t buffer [512]={0};
uint8_t name[TINY_STRING_SIZE]={0};
uint8_t desc[TINY_STRING_SIZE]={0};
uint8_t chances;
if ((conffp = fopen(filename,"r+")) == NULL)
{
exit(0);
}
while ( fgets( buffer, sizeof buffer, conffp ) != NULL ) {
printf("[>] %s", buffer);
scanres = sscanf( buffer, "%31[0-9a-zA-Z ]:%31[0-9a-zA-Z ]:%d", name, desc, &chances );
if ( scanres == 3 ) {
printf("[+] id:%d desc:%s\tname:\"%s\"\tchances:%d)\n", i, desc, name, chances);
//looter_grab(looter, i, desc, name, chances);
i++;
}
else
{
printf("ERR: scanf got: %d \n",scanres);
}
}
fclose (conffp);
谢谢,拉尔祖克