문제

I'm trying to make a program in c90 which will read some words from a txt file and copy them to a matrix array. It seems that the compilation is all right but there is a thread named "EXC_BAD_ACCESS(code=1 adress=0x68). can you help me figure out where the problem is??

int main(int argc, char *argv[]) {

FILE *input;
char words[10][30];
int i,a;

input=fopen("test.txt","rt");
for(i=0;i<10;i++){
    a = fscanf(input,"%s", words[i]);
    printf("%2d %s\n", a, words[i]);

    }
fclose(input);
return 0;

}

도움이 되었습니까?

해결책

Check that your file is actually being opened, otherwise printf() will try to print out random memory which may go beyond the bounds of what you have actually allocated and cause an error.

input = fopen("test.txt", "rt");
if (!input)
{
    perror("Failed to open file");
    exit(1);
}

You may also want to check that a == 1, i.e. that the fscanf() also succeeds.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top