简体   繁体   中英

exc_bad_access(code=1 adress=0x68) in c90 program

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 , ie that the fscanf() also succeeds.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM