![](/img/trans.png)
[英]Fscanf returns the same values in a loop once a line is reached that does not contain a value that is being scanned for
[英]fscanf() returns the same pattern in a loop
以下代碼中的fscanf函數一次又一次地在文件中匹配相同的模式,而不是繼續前進到下一個模式。 我怎樣才能解決這個問題?
void checkInput(char *fileName, int time, int* totalProcesses)
{
FILE *input;
int startTime;
int pid;
int prio;
int vruntime = 50;
input = fopen(fileName, "r");
do
{
fscanf(input, "%i start %i prio %i", &pid, &startTime, &prio);
if(startTime == time)
{
createProcess(pid, startTime, vruntime);
totalProcesses++;
printf("%s\n %i", "proccess created", pid );
}
} while ( !feof(input) );
fclose(input);
}
檢查fscanf的返回值-當它失敗時,它不會讀取任何內容,因此,如果您循環並再次嘗試,將發生相同的情況。 相反,您需要以下內容:
while(fscanf(input, "%i start %i prio %i", &pid, &startTime, &prio) == 3) {
if(startTime == time) {
createProcess(pid, startTime, vruntime);
totalProcesses++;
printf("%s\n %i", "proccess created", pid );
}
}
這也意味着您不使用feof
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.