I am not sure where I am going wrong. The file I am trying to read:
1 1 1 1 1 1 1 2 2 2
2 3 3 3 3 4 4 5 5 5
5 5 5 5 5 6 6 6 7 7
7 8 8 8 9 9 9 9 9 9
10 10 11 11 11 12 12 12 12 12
12 13 13 13 14 14 14 15 15 15
15 15 15 16 16 16 16 17 18 18
18 18 18 19 19 19 19 19 19 20
20 20 20 20 20 21 21 21 21 21
22 22 23 23 24 25 25 25 26 26
This is my code:
#include<stdio.h>
using namespace std;
int main(){
FILE *fptr;
char FILENAME[100];
printf("Enter the name of the file:");
gets(FILENAME);
fptr=fopen(FILENAME,"r");
if(fptr==NULL){
printf("File could not be opened");
}
int count=0,space=0;
while ((count = fgetc(fptr)) != EOF)
{
if (count == ' ')
space++;
}
printf("Number of integers in file: %d \n\n",space);
int storedData[space];
for(int i=0;i<space;i++){
fscanf(fptr,"%d", &storedData[i]);
}
for(int i=0;i<space;i++){
printf("%d \n", storedData[i]);
}
return 0;
} ```
**Output**
Enter the name of the file:Listofnumbers.dat
Number of integers in file: 100
0
0
0
0
-810195648
22737
-83690506
32763
10904080
0
34
0
-83207872
32763
56
0
0
0
0
0
34
0
-83690617
32763
1
0
0
0
1
0
0
0
0
0
-83696309
32763
0
0
0
0
8
0
16
0
34
0
-83529122
32763
1
0
-83232208
32763
0
0
-810189776
22737
34
0
-83479343
32763
-83232208
32763
-83232208
32763
6421816
0
-83232208
32763
34
0
-83522581
32763
6421808
0
-83232208
32763
1
0
-83232208
32763
-83232112
32763
70
0
10884048
0
400
0
4199998
0
4210744
0
100
0
-83207872
32763
6421512
0