Hello, got a small problem , I've made code for writing 2d array to file, but when I try to read back it to array, compiler doesn't see column and rows count in file, can someone explane where i made mistake in code?
void kuria();//makes file of 2d matrix without problem;
int main(){
kuria();
int m,n;
ifstream f("matrica.txt");
f.open("matrica.txt");///////////////////////////////////////////////////////
f>>m>>n;//here , it doesn't reads it //// and its gives me trash value/////////////////////////
///////////////////////////////////////////////////////////////////////////////
int **matr;
void kuria(){
ofstream f("matrica.txt");
srand(time(NULL));
cout<<"Iveskite matricos stulpeliu ir eiluciu skaiciu:"<<endl;
cout<<"P.S. istrizainiu suma galima tik jai stul ir eiluciu sk vienodas"<<endl;
int a,b;
cin>>a>>b;
in my example i've assumed that the array row and col information is on the first line of the file - edit accordingly if not.
here you're opening the file twice - once to get the row and col info and then going back out of the file to create the 2d array. otherwise the 2d array would be locally scoped to the file reading loop and not much use beyond that
now when you come back into the file after creating the 2d array the row and col info is still on the first line of the file - use getline() and read the whole first line into a (dummy) string so that the newline character is removed as well and only then proceed to reading the numbers into the 2d array: