有人能解释一下这个函数是做什么的吗?有没有可能更有效地编写它?我不明白,尤其是if
测试。
我所看到的:它只从文本文件中加载数字,这些数字存储到数组的指针中
void LoadNumbers(int*pNumberArray,FILE*textFile) //---F7-
{
int i = 0;
while (fscanf(textFile,"%d", &pNumberArray[i]) != EOF)
{
if (fscanf(textFile,"%*[^-+0-9]", &pNumberArray[i]) == EOF) break;
else if (pNumberArray[i] != 0 ){++i; pNumberArray[i] = '\0';
}
}
1条答案
按热度按时间tvz2xvvm1#
LoadNumbers()
zero or more integers fromtextFile
intopNumberArray
. 0 values are skipped. It appears the input file consist of an integer followed by something that is not +, - or a number. Upon exit the last element or two are 0 aka '\0'.You could read both numbers with one
fscanf()
and you could deal with the terminating 0 after the loop. Alternatively, the function could return how many values are read instead of relying on 0 as the sentinel.With the example 1.txt input file:
Both the original and the revised function returns: