im trying to fill a 2D array from a .txt file the file has a 20x20 grid with random numbers and this is my code.
// open file
ofstream myfile;
myfile.open("test.txt");
// test if file is read
if (!myfile)
{
//display something if file not read
cout << "File not Read"<< endl; // !Working
}
else
{
// test if file is open
cout << "file Read"<< endl; // !Working
}
//initialize array
int array[20][20];
// Filling up the array from .txt
for (int i = 0; i < 20 && myfile.good(); ++i) // Less than 25 elements, and ifstream is still good.
{
for (int j = 0; j < 20; ++j){
myfile >> array[i][j];
// Reads Left to right, top to bottom.
}
}
im trying to fill up the array number by number, after that im gonna do some calculations with the numbers... they need to be INTs
// test if file is read
if (!myfile)
{
//display something if file not read
cerr << "File not Read"<< endl; // !Working
exit( 1 );
}
for (int i = 0; i < 20; ++i)
{
for (int j = 0; j < 20; ++j){
myfile >> array[i][j];