Thread: reading a columns input data file

  1. #1
    Registered User
    Join Date
    Apr 2003
    Posts
    5

    reading a columns input data file

    I need to read a 3 column input data file and to select (for further calculations) just the first 2 columns...

    The problem I have is that when I try to "printf" these data, the program crushes, so I'm not be able to check if my program actually reads corectly the input file...


    2.5000000000E+1 1.6285272837E+0 1.2494180202E+0
    2.5018749237E+1 1.3162814379E+0 1.9428113699E+0
    2.5037498474E+1 1.2828449011E+0 2.7128908634E+0
    2.5056249619E+1 1.5575599670E+0 2.9970877171E+0
    2.5074998856E+1 1.9759304523E+0 2.5490462780E+0
    2.5093750000E+1 2.2521975040E+0 1.6395831108E+0
    2.5112499237E+1 2.1530494690E+0 9.4248563051E-1
    2.5131248474E+1 1.7716845274E+0 1.0123537779E+0
    2.5149999619E+1 1.4909162521E+0 1.7716845274E+0


    TO be more, clear, in the first part of my program I read the file line by line (I look for '\n' delimiters), so finally I get the number of lines of the input files... This works fine!
    I get lets say, count=373, "count" being the parameter i used for the number of lines...

    Then, I just want to select fron the same file, the first two columns, as a arrays, so I wrote:

    double *x, *y, *z;

    for ( i=0; i<count; i++ )
    { fscanf (stream,"%le /t %le /t %le", &x[i], &y[i], &z[i]);
    }


    I tried then to check if the file is read correctly, so I added in the fors's above loop the function :

    printf ("\n %le", &x[i]);


    At this point, the program crushes, so I suppose something it's not correct...

    I would appreciate any suggestions or help in order to be able to "printf" the first 2 columns, so I will know my program reads correctly the data file....

    Thank you!

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>fscanf (stream,"%le /t %le /t %le", &x[i], &y[i], &z[i])
    Check the return code from this function to make sure it read in what you asked it to. I can guarantee you it isn't working

    The return code should be 3, as you are trying the scan 3 numbers.

    A tab character is \t, not /t.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    5

    checked

    Thanks, Hammer!

    I corrected the misspelling, but, still, after I include "ptrintf" function (for x, for example), to check what the programs reads, the result is

    0.000000e+000

    so, I still can't read the input file corrrectly



  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Something like this, maybe?
    Code:
    #include <stdio.h> 
    #include <stdlib.h> 
    
    int main(void) 
    {
       const char filename[] = "test.txt";
       FILE *file = fopen(filename, "r");
       if ( file != NULL )
       {
          size_t i, count, size = 10;
          double *x = malloc(size * sizeof(*x));
          double *y = malloc(size * sizeof(*y));
          double *z = malloc(size * sizeof(*z));
          if ( x != NULL && y != NULL && z != NULL )
          {
             /* Read all three columns. */
             for ( count = 0; count < size; ++count )
             {
                if ( fscanf (file ,"%lf%lf%lf", &x[count], &y[count], &z[count]) != 3 )
                {
                   break;
                }
             }
             /* Use only the first two columns. */
             for ( i = 0; i < count; ++i )
             {
                printf("x = %f, y = %f\n", x[i], y[i]);
             }
             free(x);
             free(y);
             free(z);
          }
       }
       return 0; 
    }
    [edit]
    Oops! I had to fix the code to take my own advice! (I previously had %lf in the printf.)
    [/edit]
    Last edited by Dave_Sinkula; 04-29-2003 at 07:15 AM.
    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.*

  5. #5
    Registered User
    Join Date
    Apr 2003
    Posts
    5
    Thank you for your sugestion Dave,


    At this time, I have the following:


    #include <malloc.h>

    #define CALLOC(num, type) ((double*)calloc (num, sizeof(double)))
    #define FREE(addr) (free((void*) (addr)))

    int i, count;
    double *x, *y, *z; // I'll skip the declaration of other
    // variables and the other parts
    // of the programs (within main func)

    int main (void)

    {

    FILE *stream;
    if ( (stream = fopen ( "r42.txt", "r" )) == NULL )
    { printf ("Cannot read the new file\n");
    exit (1);
    }

    count = 373; // returned by another function
    // of the program, I have 373 lines
    // in each of the 3 columns

    x=CALLOC(count, double);
    y=CALLOC(count, double);
    z=CALLOC(count, double);


    for ( i=0; i<count; i++ )
    { fscanf (stream,"%le %le %le", &x[i], &y[i], &z[i]);
    printf ("\n %d %le %le %le", i, x[i], y[i], z[i]);
    }

    FREE(x);
    FREE(y);
    FREE(z);

    fclose (stream);

    }



    the printf function will give me 373 lines as follows:

    0.000000e+000 0.000000e+0 0.000000e+0



    so, I don't understand why the input file is not read

    Thank you for your help!

  6. #6
    Registered User
    Join Date
    Apr 2003
    Posts
    5

    reading 3 columns input data file

    What I want here is to read a 3 column input data file, as the folloing:



    2.5000000000E+1 1.6285272837E+0 1.2494180202E+0
    2.5018749237E+1 1.3162814379E+0 1.9428113699E+0
    2.5037498474E+1 1.2828449011E+0 2.7128908634E+0
    2.5056249619E+1 1.5575599670E+0 2.9970877171E+0
    2.5074998856E+1 1.9759304523E+0 2.5490462780E+0
    2.5093750000E+1 2.2521975040E+0 1.6395831108E+0
    2.5112499237E+1 2.1530494690E+0 9.4248563051E-1
    2.5131248474E+1 1.7716845274E+0 1.0123537779E+0
    2.5149999619E+1 1.4909162521E+0 1.7716845274E+0
    2.5168748856E+1 1.4909162521E+0 2.7529489994E+0

    this data file has 373 line, this number being returned by another function of the frogram, which works fine, so I won't put it here...

    What I want is to read this file and to use these numbers in further calculations...


    Selecting from my program whe part that is not working, this looks as follows:

    -----------------------------------------

    #include <malloc.h>

    #define CALLOC(num, type) ((double*)calloc (num, sizeof(double)))
    #define FREE(addr) (free((void*) (addr)))

    int i, count;
    double *x, *y, *z; // I'll skip the declaration of other
    // variables and the other parts
    // of the programs (within main func)

    int main (void)

    {

    FILE *stream;
    if ( (stream = fopen ( "r42.txt", "r" )) == NULL )
    { printf ("Cannot read the new file\n");
    exit (1);
    }

    count = 373; // returned by another function
    // of the program, I have 373 lines
    // in each of the 3 columns

    x=CALLOC(count, double);
    y=CALLOC(count, double);
    z=CALLOC(count, double);


    for ( i=0; i<count; i++ )
    { fscanf (stream,"%le %le %le", &x[i], &y[i], &z[i]);
    printf ("\n %d %le %le %le", i, x[i], y[i], z[i]);
    }

    FREE(x);
    FREE(y);
    FREE(z);

    fclose (stream);

    }

    ---------------------------------------------------------



    the printf function will give me 373 lines as follows:

    0.000000e+000 0.000000e+0 0.000000e+0



    so, I don't understand why the input file is not read

    Thank you for your help!

  7. #7
    Registered User
    Join Date
    Apr 2003
    Posts
    5

    thank you all, guys!

    Thank you all guys for your help...

    I got it work.... finally, it turned out to be just a stupid memory problem...
    I tworks fine now!

    vk

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. input data from file into matrix?
    By cuizy in forum C Programming
    Replies: 1
    Last Post: 04-16-2009, 05:06 AM
  2. Reading from a file and using the data
    By Ste_Mulv in forum C Programming
    Replies: 3
    Last Post: 04-01-2009, 07:44 AM
  3. Checking array for string
    By Ayreon in forum C Programming
    Replies: 87
    Last Post: 03-09-2009, 03:25 PM
  4. opening empty file causes access violation
    By trevordunstan in forum C Programming
    Replies: 10
    Last Post: 10-21-2008, 11:19 PM
  5. Please Help Reading Data From A File
    By NickHolmes in forum C Programming
    Replies: 5
    Last Post: 05-29-2005, 11:24 PM