Read text file into an array

closed account (DSvMDjzh)
So first off I need to take data from a text file and put it into a multidimensional array. I have one program that puts the array into text using
1
2
3
4
5
6
7
8
9
10
	
ofstream myfile;
myfile.open ("array.txt");						
for(int i=0;i<65000;i++){
myfile << Data[i][0];
myfile << " ";
myfile << Data[i][1];
myfile << "\n";
}
myfile.close();

Essentially I want it back into that form. I am at a complete loss of how to do it.

Thanks Josh
Just do the reverse operation. So use an ifstream instead of an ofstream and read the data from that stream to the array.

http://www.cplusplus.com/reference/iostream/ifstream/
http://www.cplusplus.com/reference/iostream/istream/operator%3E%3E/
Last edited on
1
2
3
4
5
6
7
8
{
    ifstream fin( "array.txt" );
    for( int i = 0; i < 65000; ++i )
    {
        fin >> Data[i][0];
        fin >> Data[i][1];
    }
}

Note that the success of lines 5 and 6 should be tested. As it stands it assumes they always succeed (which they should with valid data).
closed account (DSvMDjzh)
This is what I have now
1
2
3
4
5
6
7
8
	
ifstream myfile;
for(int i=0;i<65000;i++){
myfile.open ("array.txt");						
Data[i][0] = myfile.get();
Data[i + 65000][1] = myfile.get();
}
myfile.close();

It doesn't work,
I don't know how to get the values from the text file and put them into the array.
closed account (DSvMDjzh)
Just saw moorecm post... I will try that and report back.

Thanks
closed account (DSvMDjzh)
It works, thanks for the help

Josh
Topic archived. No new replies allowed.