How to read numercial data from a file

Discussion in 'C++' 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. On Sun, 11 Jul 2004 16:15:53 +0800, 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!
    >
    > Dennis
    >

    Well its not so difficult in C++ (I assume you mean C++ not C, since you
    posted to a C++ group), You just declare the array and read them. The only
    difference is that you must read in some sort of loop.

    #include <fstream>

    std::ifstream file("data1.txt");
    double a[10];
    for (int i = 0; i < 10; ++i)
    file >> a;

    If you want a C answer post to news:comp.lang.c

    john
     
    John Harrison, Jul 11, 2004
    #2
    1. Advertising

  3. John Harrison wrote:

    > On Sun, 11 Jul 2004 16:15:53 +0800, 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

    [snip]

    >> Dennis
    >>

    > Well its not so difficult in C++ (I assume you mean C++ not C, since
    > you posted to a C++ group), You just declare the array and read them.
    > The only difference is that you must read in some sort of loop.
    >
    > #include <fstream>
    >
    > std::ifstream file("data1.txt");
    > double a[10];
    > for (int i = 0; i < 10; ++i)
    > file >> a;
    >
    > If you want a C answer post to news:comp.lang.c
    >
    > john
    >
    >


    Actually, he did. Perhaps, I didn't pay attention
    to the time and date stamps of these posts. ;-)


    --
    Thomas Matthews

    C++ newsgroup welcome message:
    http://www.slack.net/~shiva/welcome.txt
    C++ Faq: http://www.parashift.com/c -faq-lite
    C Faq: http://www.eskimo.com/~scs/c-faq/top.html
    alt.comp.lang.learn.c-c++ faq:
    http://www.raos.demon.uk/acllc-c /faq.html
    Other sites:
    http://www.josuttis.com -- C++ STL Library book
     
    Thomas Matthews, Jul 12, 2004
    #3
    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. Doug
    Replies:
    3
    Views:
    4,404
    Bruce Barker
    Nov 4, 2005
  2. sweety
    Replies:
    9
    Views:
    1,056
    Richard Heathfield
    Feb 7, 2006
  3. Replies:
    2
    Views:
    647
    Thomas Matthews
    Feb 27, 2007
  4. Sreejith K
    Replies:
    24
    Views:
    1,067
    Sreejith K
    Mar 24, 2009
  5. Alex Dowad
    Replies:
    4
    Views:
    300
    Michel Demazure
    May 1, 2010
Loading...

Share This Page