I have a data file called proj3input.dat and it has 10000 entries that I have to use to fill an array. I am having trouble getting the data from the file into the array. This code (sorry about the ugly formatting, it completely changed with i pasted into stackoverflow) currently prints an array full of 0s, and i cannot see where my error is. The data points are all decimals, not integers and for now I just need to get the data points to fill my array, data[10000].
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#define MAX_FILE_NAME 100
int main(int argc, char *argv[])
{
FILE *proj3input, *output;
int i, count = 0, max_window, window_size, p, m, q, x;
int *num_windows;
double time[10000], data[10000], data_mean, data_sum;
char filename[MAX_FILE_NAME];
char c;
/* Open files */
proj3input = fopen("proj3input.txt", "r");
output = fopen("output.dat", "w"); /* prototype any functions! */
/*Verify file*/
if (proj3input == (FILE *)NULL) {
printf("**********ERROR**********\n");
printf("* Cannot open required *\n");
printf("* file. Please check *\n");
printf("* your directory *\n");
printf("**********ERROR**********\n");
exit(EXIT_FAILURE);
}
for (c = getc(proj3input); c != EOF; c = getc(proj3input)) {
if (c == '\n') {
count = count + 1;
}
}
printf("The file has %d lines \n", count);
max_window = count;
printf("Please enter the size of the window you require.\n");
printf("This must be an integer greater than 1 and less than %d\n",
max_window);
scanf("%d", &window_size);
/* If validation failed ... */
/*if(!validInput)
{
printf(" ERROR: No input detected. Please input a positive integer. \n");
return(EXIT_FAILURE);
}*/
/* Here we check that we have a valid window size*/
if (window_size == 0) {
printf("**********ERROR**********\n");
printf("* Input value must be *\n");
printf("* nonzero. Please run *\n");
printf("* again with non zero *\n");
printf("* input *\n");
printf("**********ERROR**********\n");
}
if (num_windows == NULL) {
printf("Memory allocation failure. Exiting...\n");
return (EXIT_FAILURE);
}
for (i = 0; i <= 10000; i++) {
fscanf(proj3input, "%lf", &data[i]);
}
/* Close the file */
fclose(proj3input);
for (x = 0; x < 10; x++) {
printf("%lf\n", data[x]);
}
return 0;
}
fscanf; doing so will clue you into what's going wrong.rewindit before reading the floating-point numbers. (fscanfshould returnEOFevery time you call it in your loop. Please make use of its return value. For example, your code won't run as expected if you have blank lines.)char c;-->>int c;