How to read data from a txt file into an array HELP!

I wanna read a txt file like this
1.00518 2.01903 3.01139 4.01343 5.02751 5.99913 7.00011 7.99851 .....
and fill the array P[i][j] with the data.

I try to use
if (myfile.is_open())
{
//for (int i = 0; i < 1; i++)
//{
getline (myfile,DMArray[0]);
cout << DMArray[i] << endl;
//}
myfile.close();
}

But this read the data into a string. How should i do with this string, any suggestion?
Besides I know the dimension of the data I am reading.
Last edited on
First off, you are always inputting the data into the first element in DMArray,... you will need to change the DMArray[0] to DMArray[i]. I'm assuming the DMArray is of type double? What you can do is use a temp string to input it, then convert the string to double.
Like so:
1
2
3
4
5
6
7
8
9
10
11
string temp = " ";
if (myfile.is_open())
{
for (int i = 0; i < SIZE && myfile.good(); i++)
{
getline(myfile, temp);
DMArray[i] = atof(temp.c_str());
cout<<DMArray[i]<<endl;
}
myfile.close();
}
Topic archived. No new replies allowed.