我想从文件中读取所有整数并将它们全部放入数组中
./prog input.txt
其中 input.txt 包含例如以下数字
-6 8 9 0 45 54 67 0 12
23 3 -25 12 67 6 9 -9
如何在事先不知道整数个数的情况下执行此操作
谢谢大家的回答!
最佳答案
类似这样的事情。 (未测试)
while(!feof(inFile)){ //inFile is your pointer to the file opened with fopen()
fscanf(inFile,"%d",&a[i]); //a is your array
i++;
}
这样,在您的 i
变量中,您将获得文件中的项目数 (+1),并且您将获得数组中的所有数字。
关于从文件中读取数字的 C 代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7779061/