Newbie question: loading data from text file

Discussion in 'C++' started by philbo30, Sep 16, 2007.

  1. philbo30

    philbo30 Guest

    This should be easy; I have data in a text file arranged in a single
    column that looks like this:

    ..123
    ..456
    ..789
    ..010
    ..936
    ..248
    ..235
    ..801


    I need to open the file and extract the data into something (an array
    perhaps) that looks like this:

    double data [] = {.123, .456, .789, .010, .936, .248, .235, .801};

    Opening the file is easy, but how do I load the array of values? Note
    that a "\n" follows each of the values in the column.
    philbo30, Sep 16, 2007
    #1
    1. Advertising

  2. philbo30 wrote:
    > This should be easy; I have data in a text file arranged in a single
    > column that looks like this:
    >
    > .123
    > .456
    > .789
    > .010
    > .936
    > .248
    > .235
    > .801
    >
    >
    > I need to open the file and extract the data into something (an array
    > perhaps) that looks like this:
    >
    > double data [] = {.123, .456, .789, .010, .936, .248, .235, .801};
    >
    > Opening the file is easy, but how do I load the array of values? Note
    > that a "\n" follows each of the values in the column.


    What whitespace separates values does not matter. Just use >>
    like so:

    vector<double> data;
    double d;
    while (in >> d) { // presuming 'in' is the ifstream
    data.push_back(d);
    }

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
    Victor Bazarov, Sep 16, 2007
    #2
    1. Advertising

  3. philbo30

    philbo30 Guest

    On Sep 16, 3:08 pm, "Victor Bazarov" <> wrote:
    > philbo30 wrote:
    > > This should be easy; I have data in a text file arranged in a single
    > > column that looks like this:

    >
    > > .123
    > > .456
    > > .789
    > > .010
    > > .936
    > > .248
    > > .235
    > > .801

    >
    > > I need to open the file and extract the data into something (an array
    > > perhaps) that looks like this:

    >
    > > double data [] = {.123, .456, .789, .010, .936, .248, .235, .801};

    >
    > > Opening the file is easy, but how do I load the array of values? Note
    > > that a "\n" follows each of the values in the column.

    >
    > What whitespace separates values does not matter. Just use >>
    > like so:
    >
    > vector<double> data;
    > double d;
    > while (in >> d) { // presuming 'in' is the ifstream
    > data.push_back(d);
    > }
    >
    > V
    > --
    > Please remove capital 'A's when replying by e-mail
    > I do not respond to top-posted replies, please don't ask


    What would be the alternative to vector? When I try to use the above,
    later in the code, I get the following error during compilation with g+
    +:

    no matching function for call to 'DoubleArray::DoubleArray
    (vector<double, allocator<double> > &, unsigned int)'

    candidates are: DoubleArray::DoubleArray ()
    DoubleArray::DoubleArray(const double *,int)
    DoubleArray::DoubleArray(conts DoubleArray &)

    I just started to work with C++ today, but I have decent experience
    with C.
    philbo30, Sep 16, 2007
    #3
  4. philbo30

    James Kanze Guest

    On Sep 16, 8:51 pm, philbo30 <> wrote:
    > This should be easy; I have data in a text file arranged in a single
    > column that looks like this:


    > .123
    > .456
    > .789
    > .010
    > .936
    > .248
    > .235
    > .801


    > I need to open the file and extract the data into something (an array
    > perhaps) that looks like this:


    > double data [] = {.123, .456, .789, .010, .936, .248, .235, .801};


    > Opening the file is easy, but how do I load the array of values?


    std::vector< double > v(
    (std::istream_iterator< double >( file )),
    (std::istream_iterator< double >()) ) ;

    > Note that a "\n" follows each of the values in the column.


    Streams use white space as a separator, skipping it.

    --
    James Kanze (GABI Software) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
    James Kanze, Sep 16, 2007
    #4
  5. philbo30

    James Kanze Guest

    On Sep 16, 9:53 pm, philbo30 <> wrote:
    > On Sep 16, 3:08 pm, "Victor Bazarov" <> wrote:

    [...]
    > What would be the alternative to vector? When I try to use the above,
    > later in the code, I get the following error during compilation with g++:


    > no matching function for call to 'DoubleArray::DoubleArray
    > (vector<double, allocator<double> > &, unsigned int)'


    > candidates are: DoubleArray::DoubleArray ()
    > DoubleArray::DoubleArray(const double *,int)
    > DoubleArray::DoubleArray(conts DoubleArray &)


    If you use std::vector, you have to use std::vector. The
    idiomatic solution would be to give DoubleArray a template
    constructor:

    template< typename Iterator >
    DoubleArray::DoubleArray(
    Iterator begin,
    Iterator end )

    But before going any further, obviously, we would have to know
    more about DoubleArray. If you give it the above constructor,
    then you may not need the vector to begin with; you can
    initialize it directly from the std::istream_iterator. Provided
    it is written to work with input iterators, and not just random
    access iterators.

    If you need to access legacy code, which you cannot change, and
    which expects a double*, int, you can use &vect[0] and
    vect.size().

    > I just started to work with C++ today, but I have decent
    > experience with C.


    Forget your C experience to begin with. It will come into good
    use later, when you've mastered the basic idioms of C++, but
    typically (and especially where collections of data are
    concerned), good, idiomatic C++ is very, very different from C.
    (You might want to get "Accelerated C++", and try working
    through it quickly, pretending you don't know C.)

    --
    James Kanze (GABI Software) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
    James Kanze, Sep 16, 2007
    #5
  6. philbo30

    BobR Guest

    BobR, Sep 17, 2007
    #6
    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. kwaj
    Replies:
    1
    Views:
    500
  2. David Segall
    Replies:
    2
    Views:
    426
    Thomas Kellerer
    Jan 2, 2007
  3. philbo30
    Replies:
    5
    Views:
    336
    Keith Thompson
    Jul 30, 2006
  4. Replies:
    4
    Views:
    296
  5. Al-Bot
    Replies:
    1
    Views:
    124
    Thomas 'PointedEars' Lahn
    Dec 23, 2005
Loading...

Share This Page