My goal is to read in a txt file (small_ramp.txt) then input all the numbers excluding the first into an array.The file has 11 lines of numbers on it, the number 10 on the first line indicating how many numbers are in the file then a following 10 numbers (1-10)Small_ramp.txt. When in command prompt i enter in Stats.exe < small_ramp.txt when executing this command I Error receive this error message.
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char* argv[])
{
int n, i;
FILE* fpointer;
double *arr;
fpointer = fopen(argv[1], "r");
if (argc != 2) {
printf("ERROR: you must specify file name!\n");
return 1;
}
if (!fpointer) {
perror("File open error!\n");
return 1;
}
fscanf(fpointer, "%d", n);// Scan first line of small_ramp.txt to find array size
arr = (double*)malloc(sizeof(double) * n);// allocate memory for array with the size given (n)
while (!feof(fpointer)) {
fscanf(fpointer, "%lf", arr + 1);
}
for (int i = 0; i < n; ++i)
{
printf("%lf,", arr[i]);
}
fclose(fpointer);
return 0;
}
I guess my question is am I reading in the file correctly and if so could someone point me in the right direction for fixing this error?