I have a file that contains integers separated with ' '
Example File1
8 7 8 8 7 7 7
Example file2
10 0 3 11 0 2 3 3 2 4 5 6 2 11
I want to read these integers and save them in an integer array.
-->int array[for example File1]={8,7,8,8,7,7,7}
-->int array[for example File2]={10,0,3,11,0,2,3,3,2,4,5,6,2,11}
I have done this code so far... Can anyone help here?
#include <stdio.h>
int main() {
FILE *f1;
int i,counter=0;
char ch;
f1 = fopen("C:\\Numbers.txt","r");
while((ch = fgetc(f1))!=EOF){if(ch==' '){counter++; }}
int arMain[counter+1];
for(i=0; i<counter+1; i++) {
fscanf(f1, "%d", &arMain[i]);
}
for(i = 0; i <= counter+1; i++) {
printf("\n%d",arMain[i]);
}
return 0;
}