我必须从文件行中解析数据,为此,我现在使用strtol()函数。
例如,我在文本文件中包含以下行:1 abc
例如,这是无效的行,因为此特定行必须包含一个且只有一个整数值。
现在,当我以这种方式使用strtol时:
FILE *fs;
fs = fopen("file.txt","r");
char* ptr = NULL; // In case we have string except for the amount
char lineInput[1024]; // The max line size is 1024
fscanf(fs,"%s",lineInput);
long numberOfVer = strtol(lineInput,&ptr,BASE);
printf("%lu\n%s\n",numberOfVer,ptr); // Here I test the values,I expect to get 1 followed by newline and abc
if (numberOfVer == 0 || *ptr != '\0') { // Not a positive number or there exists something after the amount!
fprintf(stderr,INVALID_IN);
return EXIT_FAILURE;
}
但是,ptr字符串不是“ abc”或“ abc”,而是一个空字符串... 这是为什么?根据文档,它必须是“ abc”。