數組我有此格式的文本文件:閱讀從文本文件的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(文件)不正確,那麼循環遍歷文件的每一行的正確方法是什麼? –