내 C 프로그램의 fclose()가 잘못된 액세스를 유발하는 이유를 알 수 없습니다. 그것은 잘 작동하고 문자열을 서로 같지 않을 때 인쇄 할 if 조건을 변경하고 갑자기 문제가 발생하기 시작했습니다. 떨어져 나쁜 액세스 오류에서, 그것은 또한 "NEWFILE.TXT"fclose가 exc_bad_access를 일으키는 경우
#include<stdio.h>
#include<stdlib.h>
#include <string.h>
int main()
{
FILE * cFile;
FILE *outputfile;
FILE *newfile;
cFile = fopen("input.in", "r");
if (cFile == NULL){
printf("bad input file");
}
newfile = fopen("newfile.txt", "w+");
if (newfile == NULL){
printf("bad newfile");
}
char tempstring[15];
char tempstring2[15];
//get each line in the cFile
while (fscanf(cFile, "%15s", tempstring) != EOF) {
outputfile = fopen("outputlotnum.txt", "r"); //open/(or reopen) outputfile to check lines
if (outputfile == NULL){
printf("bad outputfile");
}
//get each line in the outputfile
while(fscanf(outputfile, "%15s", tempstring2) != EOF){
//if the line from cFile doesn't match the line from outputfile,
//then go ahead and print the line to the newfile.txt
if (strcmp(tempstring, tempstring2) != 0){
fprintf(newfile,"%15s \n", tempstring2);
}
//else don't print anything and continue on to the next line
}
fclose(outputfile); //close the outputfile after checking all the lines for a match
}
fclose(newfile); //throws bad access
fclose(cFile);
return 0;
}
'fopen()'이 실패하면'NULL'을 반환합니다. ** 확인해보십시오. ** –
나는 그것들을 검사하지 않았고 아무도 NULL을 반환하지 않았습니다. – user1066524
세 파일이 이미 있기 때문에 정말 이상합니다. 세 개 중 두 개는 이미 어떤 종류의 텍스트로 채워져 있으며 newfile은 비어있는 텍스트입니다. – user1066524