Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Newbie question: loading data from text file

Reply
Thread Tools

Newbie question: loading data from text file

 
 
philbo30
Guest
Posts: n/a
 
      09-16-2007
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.

 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      09-16-2007
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


 
Reply With Quote
 
 
 
 
philbo30
Guest
Posts: n/a
 
      09-16-2007
On Sep 16, 3:08 pm, "Victor Bazarov" <v.Abaza...@comAcast.net> 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:oubleArray
(vector<double, allocator<double> > &, unsigned int)'

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

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


 
Reply With Quote
 
James Kanze
Guest
Posts: n/a
 
      09-16-2007
On Sep 16, 8:51 pm, philbo30 <masfe...@gmail.com> 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

 
Reply With Quote
 
James Kanze
Guest
Posts: n/a
 
      09-16-2007
On Sep 16, 9:53 pm, philbo30 <masfe...@gmail.com> wrote:
> On Sep 16, 3:08 pm, "Victor Bazarov" <v.Abaza...@comAcast.net> 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:oubleArray
> (vector<double, allocator<double> > &, unsigned int)'


> candidates are: DoubleArray:oubleArray ()
> DoubleArray:oubleArray(const double *,int)
> DoubleArray:oubleArray(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:oubleArray(
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


 
Reply With Quote
 
BobR
Guest
Posts: n/a
 
      09-16-2007

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


What James said.
You might be interested in this:

Get "Thinking in C++", 2nd ed. Volume 1&2 by Bruce Eckel
(available for free here. You can buy it in hardcopy too.):
http://www.mindview.net/Books/TICPP/...ngInCPP2e.html

This FAQ has some good tips.
FAQ http://www.parashift.com/c++-faq-lite

--
Bob R
POVrookie


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[OT] Is loading the second Java application faster than loading the first? David Segall Java 2 01-02-2007 04:41 PM
Newbie question on loading data from config file philbo30 C Programming 5 07-30-2006 09:12 PM
Loading Data from Text File into an HTML Drop Down Box Al-Bot Javascript 1 12-23-2005 11:24 AM
Image loading using javascript. Handling timeouts and parrallel loading under IE zborisau@gmail.com Javascript 4 08-28-2005 02:02 PM
Loading Data from Text File kwaj VHDL 1 03-02-2004 11:12 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57