filling an array from .txt

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
i can read the file perfectly it just shows me a "no match for opperator >>" in the part where im filling up the array

"myfile >> array[i][j]"
The file needs to be an ifstream in order to do file input.
could you give me an example, im lost
ofstream myfile;

This needs to be an ifstream.
ok i changed it!

but its not loading my array


#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
#include <cstdlib>


using namespace std;

int main() {

// Open file
ifstream myfile("test.txt",ios::in);

// 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];


}
cout << endl;
}

myfile.close();
}
Last edited on
Topic archived. No new replies allowed.