Reading text file into an array

Discussion in 'C' started by WAhamed, Aug 28, 2010.

  1. WAhamed

    WAhamed New Member

    Joined:
    Aug 28, 2010
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    I am new in c++.

    I need to read a text file containing something like this data:

    0.6662 0.5435 0.5545 0.55487
    0.4346 0.7698 0.8965 0.56438
    ...
    ...

    It is 100 rows and 4 columns

    I write the following codes:

    #include
    #include
    #include

    using namespace std;
    //The main program
    int main()
    {
    double str[100][4];
    string xx;
    ifstream myfile("testfile.txt");
    while(!myfile.eof()){
    getline(myfile, xx, ' ');
    for (int i = 0; i < 100; i++){
    for (int j = 0; j < 4; j++){
    str[j] = atof(xx.c_str());
    }
    }
    }
    myfile.close();
    system("pause");
    }

    I checked the matrix str, it just give me first column repeatedly. I am no where close to the answer. Please help...
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice