double: from textfile to a vector

Hey!

I started recently programming c++.
Now, I have the following problem:

I need to assign a double-field "double data[]" with the necessary data for some calculations.
This data has to be imported from a textfile: Each line represents one value (double).
And here start the difficulties:
I have this textfile with the necessary data and want to assign it to a double-field.

... but I haven't a clue, how to do that!

Maybe you have an idea, how to do that...

Using GetOpenFileName(&ofn) is the right way to do that?
Hi,
try http://www.cplusplus.com/doc/tutorial/files.html for info on reading the textfile for the data.
i guess what he/she is searching for may be:

1
2
3
4
5
6
7
8
vector <double> values(0);

while(!file.eof())
{
getline(file, buffer);
values.pushback((double)buffer);
}
...
Hey Incubbus,

yes, I guess, that's the right direction. But until now, your sourcecode doesn't work in my program...

The problem I'm dealing with:
- Compiler: 'vector' undecleared function...

To prevent possible missunderstandings:
- vector is which kind of datatype? (until now, I used only double field[])
- declarating buffer works that way: char *buffer?
- values get's only the double-content of the line, even if there are some space characters at the beginning of the line?

Greets!
vector is a container class from the STL (see http://www.cplusplus.com/reference/stl/ for more info).
They are very useful, but if the assignment says to use an array of doubles then using a vector is out:-(
You need to decide if you can use a fixed size array (if you know how many doubles you have to read) or a dynamic array (more flexible, but more complicated).
yay... sorry for that... u need to #include <vector> for that... but if it is not the best opportunity... about the dynamic array:... i would use malloc and realloc for that... (compare the msdn)...
It works!!!

Yeah! :)

Thanks to both of you!
Last edited on
Topic archived. No new replies allowed.