I am trying to write a program that loads numbers in a text file into an array. The text file is called "numbers" and the numbers are in the following format: 3,49,52,40,34 etc..
When I try to run the program, I get a "program is not responding error". Could anyone please point out what I'm doing wrong? Many thanks.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX 32
int main (void) {
FILE* fp;
fp = fopen ("numbers.txt", "r");
if (fp == NULL)
puts ("No data in file");
return 0;
int n = 0;
int i = 0;
int j = 0;
int a[MAX];
while (fscanf(fp, "%d, ", &n) > 0) {
a[i++] = n;
}
fclose (fp);
printf ("Data loaded");
for (j = 0; j < MAX; j++) {
printf ("%d", a[j++]);
printf ("THE END");
}
return 0;
}
ifcondition which causes the program to return without closing the file handle.if (fp == NULL)means you'll always execute the return 0.