How to read numerical data from a file

Discussion in 'C Programming' started by Wei-Chao Hsu, Jul 11, 2004.

  1. Wei-Chao Hsu

    Wei-Chao Hsu Guest

    There are some data files look like

    1.) data1.txt
    ----------------
    1.0 2.0 3.0
    4.0 5.0 6.0
    7.0 8.0 9.0
    10.0

    2.) data2.txt
    ---------------------
    1.0 2.0 3.0 4.0
    5.0 6.0 7.0 8.0
    9.0 10.0

    3.) data3.txt
    ----------------
    1.0 2.0
    3.0 4.0 5.0
    6.0 7.0
    8.0 9.0 10.0

    They will be store in a array. A[0]=1.0,A[1]=2.0,.....
    I never know the format in each file.
    In fortran, I just declare an array A(10), and read them
    READ(file_no,*)A
    I don't know how to do the same thing in C.
    I need your help. Thanks!

    Dennis
     
    Wei-Chao Hsu, Jul 11, 2004
    #1
    1. Advertising

  2. In 'comp.lang.c', "Wei-Chao Hsu" <> wrote:

    > There are some data files look like
    >
    > 1.) data1.txt
    > ----------------
    > 1.0 2.0 3.0
    > 4.0 5.0 6.0
    > 7.0 8.0 9.0
    > 10.0


    I'm not going to give you a working solution, but here are some hints:

    A smart combination of fgets() (reads a line of text) and strtod()
    (converts text to double) should work. Read carefully your C-book about the
    parameters and behaviour of these functions.

    --
    -ed- get my email here: http://marreduspam.com/ad672570
    The C-language FAQ: http://www.eskimo.com/~scs/C-faq/top.html
    C-reference: http://www.dinkumware.com/manuals/reader.aspx?lib=c99
    FAQ de f.c.l.c : http://www.isty-info.uvsq.fr/~rumeau/fclc/
     
    Emmanuel Delahaye, Jul 11, 2004
    #2
    1. Advertising

  3. Wei-Chao Hsu

    -berlin.de Guest

    Wei-Chao Hsu <> wrote:
    > There are some data files look like


    > 1.) data1.txt
    > ----------------
    > 1.0 2.0 3.0
    > 4.0 5.0 6.0
    > 7.0 8.0 9.0
    > 10.0


    > 2.) data2.txt
    > ---------------------
    > 1.0 2.0 3.0 4.0
    > 5.0 6.0 7.0 8.0
    > 9.0 10.0


    > 3.) data3.txt
    > ----------------
    > 1.0 2.0
    > 3.0 4.0 5.0
    > 6.0 7.0
    > 8.0 9.0 10.0


    > They will be store in a array. A[0]=1.0,A[1]=2.0,.....
    > I never know the format in each file.
    > In fortran, I just declare an array A(10), and read them
    > READ(file_no,*)A
    > I don't know how to do the same thing in C.


    Without any error checking (which you definitely should do) and
    assuming that the lines of dashes don't belong to the files:

    int i;
    double A[ 10 ];
    FILE *fp = fopen( "data1.txt", "r" );
    for ( i = 0; i < 10; i++ )
    fscanf( fp, "%lf", A + i );

    Hint: scanf() skips white space in most cases (that includes the end
    of line, i.e. '\n').
    Regards, Jens
    --
    \ Jens Thoms Toerring ___ -berlin.de
    \__________________________ http://www.toerring.de
     
    -berlin.de, Jul 11, 2004
    #3
  4. Wei-Chao Hsu

    Al Bowers Guest

    Wei-Chao Hsu wrote:
    > There are some data files look like
    >
    > 1.) data1.txt
    > ----------------
    > 1.0 2.0 3.0
    > 4.0 5.0 6.0
    > 7.0 8.0 9.0
    > 10.0
    >
    > 2.) data2.txt
    > ---------------------
    > 1.0 2.0 3.0 4.0
    > 5.0 6.0 7.0 8.0
    > 9.0 10.0
    >
    > 3.) data3.txt
    > ----------------
    > 1.0 2.0
    > 3.0 4.0 5.0
    > 6.0 7.0
    > 8.0 9.0 10.0
    >
    > They will be store in a array. A[0]=1.0,A[1]=2.0,.....
    > I never know the format in each file.
    > In fortran, I just declare an array A(10), and read them
    > READ(file_no,*)A
    > I don't know how to do the same thing in C.
    > I need your help. Thanks!
    >


    If the data is as shown in the above examples, all floating
    point numbers, you can use function fscanf to read the file.

    To declare an array:
    double array[100];
    where the number is large enough to hold the values from the file.

    Declare a variable to hold a count of the data read from the file.
    unsigned count;

    To open the file for reading, you use function fopen.
    FILE *fp = fopen("test.txt","r");

    Use function fscanf in a loop to read the file and store the file
    contents in the array.
    if(fp)
    for(count = 0; count < 100 &&
    1 == fscanf(fp,"%lf",&array[count]); count++) ;

    then close the file.
    fclose(fp);



    --
    Al Bowers
    Tampa, Fl USA
    mailto: (remove the x to send email)
    http://www.geocities.com/abowers822/
     
    Al Bowers, Jul 11, 2004
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    4
    Views:
    550
    Roedy Green
    Mar 30, 2006
  2. Jack
    Replies:
    5
    Views:
    374
    Dennis Lee Bieber
    Apr 1, 2006
  3. Jack

    best way to index numerical text data ?

    Jack, Mar 31, 2006, in forum: C Programming
    Replies:
    1
    Views:
    337
    Jack Klein
    Mar 31, 2006
  4. Jack
    Replies:
    1
    Views:
    285
    Victor Bazarov
    Apr 1, 2006
  5. MrMF

    Numerical Integration of 2D data arrays

    MrMF, Nov 7, 2008, in forum: C Programming
    Replies:
    0
    Views:
    1,814
Loading...

Share This Page