Question regarding reading data from file into arrays

This is a discussion on Question regarding reading data from file into arrays within the C Programming forums, part of the General Programming Boards category; Code: #include <stdio.h> int lines,i; double *t, *x, *P; int main(int argc, const char *const *argv) { if ( argc ...

  1. #1
    Registered User
    Join Date
    Aug 2006
    Posts
    64

    Question Question regarding reading data from file into arrays

    Code:
    #include <stdio.h>
    
    int lines,i;
    double *t, *x, *P;
    
    int main(int argc, const char *const *argv)
    {
       if ( argc > 1 )
       {
          FILE *file = fopen(argv[1], "r"); /* Get filename from command line. */
          if ( file )
          {
             int ch, prev = '\n' /* so empty files have no lines */, lines = 0;
             while ( (ch = fgetc(file)) != EOF ) /* Read all chars in the file. */
             {
                if ( ch == '\n' )
                {
                   ++lines; /* Bump the counter for every newline. */
                }
                prev = ch; /* Keep a copy to later test whether... */
             }
             fclose(file);
             if ( prev != '\n' ) /* ...the last line did not end in a newline. */
             {
                ++lines; /* If so, add one more to the total. */
             }
             printf("lines = %d\n", lines);
          }
          else
          {
             perror(argv[1]);
          }
       }
    
       t = (double *) malloc (lines * sizeof(double));
       x = (double *) malloc (lines * sizeof(double));
       P = (double *) malloc (lines * sizeof(double));
    
       {
          FILE *file = fopen(argv[1], "r");
          if ( file )
          {
    	double temp1, temp2, temp3;
    	for(i = 0; i < lines; i++)
    	  {
    	    fscanf(file, "%lf %lf %lf", &temp1, &temp2, &temp3);
    	    t[i] = temp1;
    	    x[i] = temp2;
    	    P[i] = temp3;
    	    fprintf(stderr, "%lf %lf %lf\n", t[i], x[i], P[i]);
    	  }
    	fclose(file);
          }
          else
          {
             perror(argv[1]);
          }
       }
       return 0;
    }
    Why doesn't this one work

  2. #2
    Registered User
    Join Date
    Aug 2006
    Posts
    64
    there are three lists in file that I need to read into the arrays and print out in a different file, for now file.stdout, but it is empty when I run the program, why?

  3. #3
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    You have lines with file scope and lines with scope of the nested if statement in the first part. The lines used in the second time through the file is then the one with file scope, which was implicitly initialized to zero. So the for loop the second time through does not execute.

    Perhaps the easiest fix would be to remove the inner-scoped line here.
    Code:
    int ch, prev = '\n' /* so empty files have no lines */;
    Also &#37;lf is for double is fscanf, but %f is for double in printf (in C90).

    Checking the return value of fscanf would be a good thing to add as well.

    And in C, prefer not to cast the return value of malloc. And #include <stdlib.h>.

    Out of curiosity, where did you find my line-count code?
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  4. #4
    Registered User
    Join Date
    Aug 2006
    Posts
    64
    haha, just googled it :P thanks for that btw

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 10:46 AM
  2. question about reading in strings from a file :>
    By bball887 in forum C Programming
    Replies: 8
    Last Post: 04-13-2004, 07:24 PM
  3. Replies: 2
    Last Post: 05-12-2003, 05:40 PM
  4. help with reading a data file
    By zipfur in forum C Programming
    Replies: 4
    Last Post: 11-02-2002, 06:50 PM
  5. Help reading data file...
    By Unregistered in forum C Programming
    Replies: 4
    Last Post: 02-25-2002, 12:49 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21