我有以下代码:
int check_for_non_number(char *input, int lineNum)
{
errno = 0;
char *endptr;
printf("%s\n",input);
double xnum = strtod(input, &endptr);
// IF endptr FOUND A NON-VALID ENTRY AND THAT ENTRY IS NOT THE NEW LINE CHARACTER THEN ITS AN ERROR
if((*endptr) && (*endptr != '\n'))
{
return 1;
}
// We still want to process this number, even if it is represented as inf, so inform the user in the terminal and then return 0
else if (errno == ERANGE)
{
printf("\nOPERAND IS OUT OF RANGE ON LINE %d\n",lineNum);
return 0;
}
// ELSE IF endptr FOUND A NON-VALID ENTRY AND THAT ENTRY IS THE NEW LINE CHARACTER THEN RETURN 2 TO CHECK IF IT SHOULD BE A NEW LINE
else if((*endptr) && (*endptr == '\n'))
{
return 2;
}
else
{
return 0;
}
}
input
的值来自strtok()
,并已传递到check_for_non_number
函数...
问题是当strtok读取" inf"从文本文件,我的函数返回1,暗示第一个参数是真的... 为清楚起见," inf"在文本文件中位于文本文件中的行的中间,因此在它之前和之后都有文本,并且在它之前和之后使用了strtok。
如果有人能够解释为什么strtod()
没有处理" inf"作为对此的投入将不胜感激!