Help needed Reading data from txt file

I wanna read data from txt file with space divided each value like
1.00518 2.01903 3.01139 4.01343 5.02751 5.99913 7.00011 7.99851 8.99506 9.98015 10.9901 11.992 12.9923 13.9932 14.996 16.0034 17.012 18.0255 19.0366 20.0485 21.0505 22.0664 23.0455 24.0383 25.0374 26.0439 27.0378 28.0376 29.0576 30.066

I know the size of the data is 2*500.

In matlab I can use like

fid = fopen('example.txt');
data = fscanf(fid,'%f,',1499500);

how should I write in C++?

Any suggestions are appreciated.
closed account (Dy7SLyTq)
fstream
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <fstream>
#include <algorithm>
#include <iterator>
#include <vector>

int main()
{
    typedef std::istream_iterator<double> iter ;

    std::vector<double> data ;

    std::ifstream in("example.txt") ;

    std::copy( iter(in), iter(), std::back_inserter(data) ) ;
}
Thank you so much cire. Great help! Can I add you friend
Topic archived. No new replies allowed.