0

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;
}
3
  • 1
    You might want to try checking the return value of fscanf; doing so will clue you into what's going wrong. Commented Jan 11, 2015 at 16:22
  • After you have scanned the file for new-line characters, you are at the end of the file. You rewind it before reading the floating-point numbers. (fscanf should return EOF every 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.) Commented Jan 11, 2015 at 16:22
  • 1
    char c; -->> int c; Commented Jan 11, 2015 at 16:29

1 Answer 1

3

The loop you use to count the lines reads in the entire file:

for(c= getc(proj3input); c != EOF; c = getc(proj3input)){
    if(c == '\n'){
        count = count + 1; 
        }
}

So when you go to read it in again, you're already at the end of the file and are unable to read further. You need to add a rewind call after the initial line count:

rewind(proj3input);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.