数组我有此格式的文本文件:阅读从文本文件的int到用C
100 0 10 1
101 6 10 1
102 8 4 1
103 12 20 1
104 19 15 1
105 30 5 1
106 35 10 1
我需要把这些号码入阵列具有PID [],抵达[],突发[]和优先级[]。 C不是我最强的语言,所以我在做这件事时遇到了一些麻烦。
这里是我当前的代码:
void readFile(int n, int pID[], int arrival[], int bursts[], int priority[]){
FILE *file;
int i = 0;
file = fopen("Process.txt", "r");
//File format is pID, arrival, bursts, and priority
if (file){
while (!feof(file)){
pID[i] = fscanf(file, "%d ", &i);
arrival[i] = fscanf(file, "%d ", &i);
bursts[i] = fscanf(file, "%d ", &i);
priority[i] = fscanf(file, "%d ", &i);
}
fclose(file);
}
感谢您的帮助!
[为什么 - 虽然feof文件总是错](http://stackoverflow.com/questions/5431941/why-is-while-feof-file-always-wrong)。而且“我在做这件事上遇到了一些麻烦”并不是真的**具体*。见[问]。 – Olaf
'fscanf'返回*成功扫描的项目数*,而不是扫描值。 'if(fscanf(file,“%d”,&pID [i])!= 1)exit(1);' –
抱歉,放入数组的值不在文件附近。感谢这篇文章。如果!feof(文件)不正确,那么循环遍历文件的每一行的正确方法是什么? –