I'm trying to read in a text file that is 200 x 1000 of numbers into an array. Each number is separated by a tab. I thought using a 2D array would be good for this situation since it would allow me to distinguish between the individual rows. Being able to distinguish between the individual rows is important in this situation which is why I wanted to do it in this manner. Right now I have the following:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
ifstream file("text.txt");
if(file.is_open())
{
string myArray[1000];
for(int i = 0; i < 1000; ++i)
{
file >> myArray[i];
cout << myArray[i] << endl;
}
}
}
Which currently scans the first row of numbers into the array and then prints it out. I wanted to have a 2D array that scans each individual row into the array but keeps them separate. This means the content of row 1 is separate from the content of row 2. I figured a 2D array would do this. I am a bit stuck on this part. I attempted to make a 2D array through the use of a nested for loop but when I tried to copy the values over into the array things started to go wrong. The order was incorrect and the rows were not separated. I am coding this in C++. If someone could help me understand how to import a text document like the one I described and send all that information to a 2D array I would greatly appreciate it. Thanks.