How To Parse String File Txt Into Array With C++

Posted by Ibnu Syuhada on Stack Overflow See other posts from Stack Overflow or by Ibnu Syuhada
Published on 2013-06-30T16:01:23Z Indexed on 2013/06/30 16:21 UTC
Read the original article Hit count: 161

Filed under:

I am trying to write a C++ program, but I am not familiar with C++. I have a .txt file, which contains values as follows:

0
0.0146484
0.0292969
0.0439453
0.0585938
0.0732422
0.0878906

What I have done in my C++ code is as follows:

#include <iostream>
#include <fstream>

using namespace std;

int main()
{
    string line;
    ifstream myReadFile;
    myReadFile.open("Qi.txt");
    if(myReadFile.is_open())
    {
        while(myReadFile.good())
        {
            getline(myReadFile,line);
            cout << line << endl;
        }
        myReadFile.close();
    }
    return 0;
}

I would like to make the output of the program an array, i.e.

line[0] = 0
line[1] = 0.0146484
line[2] = 0.0292969
line[3] = 0.0439453
line[4] = 0.0585938
line[5] = 0.0732422
line[6] = 0.0878906

© Stack Overflow or respective owner

Related posts about c++