0
나는 문제에 직면하고있는 동안 나는 그것을 디버깅을 시도했지만 내가, 내가 성공적으로 5 개 값을 밀어 수있는 밀어 부분을 제거분할 오류가 큐에서 터지는는
#include <stdio.h>
#include <stdlib.h>
struct Process {
int id;
int at;
int bt;
int rt;
struct Process *next;
} *tmp;
struct Queue {
struct Process *head, *tail;
};
struct Process *pop(struct Queue *queue) {
if (queue->head == NULL) {
return NULL;
}
tmp = queue->head;
queue->head = queue->head->next;
return tmp;
}
int main() {
struct Queue queues[3];
struct Process *working;
for (int i = 0; i < 3; i++) {
queues[i].head = NULL;
}
FILE *processesFile = fopen("processes.txt", "r");
while (!feof(processesFile)) {
fscanf(processesFile, "%d %d %d", &id, &at, &bt);
push(&queues[0], id, at, bt);
}
working = pop(&queues[0]); // HERE IS THE PROBLEM, It is removing the first element but causing segmentation error
return 0;
}
아무것도 찾을 수 없었다 아무런 문제없이 전체 큐를 출력하십시오.
문제는 아마도 푸싱 부분에있을 것입니다. 예를 들어, push 할 때'next' 필드를 NULL로 설정하지 않으면. 우리가 볼 수 있도록 게시물을 편집 할 수 있습니까? –
이것이 해결책인지, 따라서 코멘트인지 나는 모른다. 두려운'feof '를 잘못 사용하고 있습니다. 파일의 끝을 알리는 것은 아닙니다. [while (! feof (file)) "이 항상 잘못된 이유는 무엇입니까?] (http://stackoverflow.com/questions/5431941/why-is-while-feof-file-always-wrong). 'fscanf'에서 리턴 값을 필수적으로 체크했다면이 에러는 트랩되었을 것입니다. '(fscanf (processesFile, "% d % d % d", & id, & at, & bt)! = 3) {/ * 트랩 오류 * /};'. 루프를 작성하는 관용적 방법은'while (fscanf (processesFile, "% d % d % d", & id, & at, & bt) == 3) {/ * process * /}' –
@ Jean-FrançoisFabre "다음"을 NULL로 설정하십시오. 고맙습니다. – Ambitions