jeudi 26 février 2015

c - fclose() causes a crash



I have a problem in this code, it executes successfully until reach fclose(fp) statement, it crashes.



void read_file(const char *filename) {
FILE *fp;
int num, i=0;

fp = fopen("numbers.txt","r");

if (fp == NULL) {
printf("Couldn't open numbers.txt for reading.\n");
exit(0);
}

int *random = malloc(sizeof(int));
if (random == NULL) {
printf("Error allocating memory!\n");
return;
}
while (fscanf(fp, "%d", &num) > 0) {
random[i] = num;
i++;
}
printf("\nTEST Before close");

fclose(fp);

printf("\nTEST After fclose");
}


The sentence TEST Before close is printed successfully and then the console is stopped printing so TEST After fclose doesn't printed and the cursor start blinking !


Any help? Thanks in advance.




Aucun commentaire:

Enregistrer un commentaire