Newbie question: loading data from text file

P

philbo30

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.
 
V

Victor Bazarov

philbo30 said:
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
 
P

philbo30

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

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

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.
 
J

James Kanze

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

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 said:
Note that a "\n" follows each of the values in the column.

Streams use white space as a separator, skipping it.
 
J

James Kanze

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.)
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top