reading dynamic array from txt file

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?

#include <cstdlib>
#include <iostream>
#include <ctime>
#include <fstream>

using namespace std;

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;


if(f.is_open())
cout<<"file is open"<< endl;

cout<<"m="<<m<<endl;
cout<<"n="<<n<<endl;

matr=new int *[m];

for(int i=0;i<m;i++)
matr[i]=new int [n];

cout<<endl;
cout<<"m="<<m<<endl;
cout<<"n="<<n<<endl;

for(int i=0;i<m;i++)
for(int j=0;j<n;j++)
f>>matr[i][j];

for (int i=0; i<m; i++){
for(int j=0; j<n; j++)
cout<<*(*(matr+i)+j)<<"\t";
cout<<endl;
}

for(int i=0;i<m;i++)
delete[]matr[i];
delete[]matr;


// eil(matr,m,n);

// cout<<"eiluciu suma ="<<sum2<<endl;



f.close();
system("pause");
return 0;


}

/*int eil(int** p,int k,int l)
{
int sum=0;
cout<<"k="<<k<<endl;
cout<<"l="<<l<<endl;
for(int i=0;i<k;i++)
for(int j=0;j<l;j++)
sum=sum+p[i][j];


}*/





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;

int **matrica;
matrica=new int *[a];

for(int i=0;i<a;i++)
matrica[i]=new int [b];


for(int i=0;i<a;i++)
for(int j=0;j<b;j++)
*(*(matrica+i)+j)=rand()%100;
cout<<"a="<<a<<endl;
cout<<"b="<<b<<endl;
for(int i=0;i<a;i++){
for(int j=0;j<b;j++){

f<<*(*(matrica+i)+j)<<"\t";
}if (i!=a-1)f<<endl;
}
cout<<"matrica:"<<endl<<endl;
for (int i=0; i<a; i++){
for(int j=0; j<b; j++)
cout<<*(*(matrica+i)+j)<<"\t";
cout<<endl;
}

f.close();

for (int i=0; i<a; i++)
delete[]matrica[i];
delete[]matrica;

}


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:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include <iostream>
#include <string>
#include <fstream>

int main()
{
    std::ifstream inFile{"F:\\test1.txt"};
    size_t row{}, col{};

    if(inFile)
    {
        inFile >> row >> col;
    }
    std::cout << "Rows: " << row << " Columns: " << col << "\n";
    int** ary = new int*[row];
    for(size_t i = 0; i < row; ++i)
    {
        ary[i] = new int[col];
    }

    while(inFile)
    {
        std::string dummy{};
        getline(inFile, dummy);
        for (size_t i = 0; i < row; ++i)
        {
            for (size_t j = 0; j < col; ++j)
            {
                inFile >> ary[i][j];
            }
        }
    }
    for (size_t i = 0; i < row; ++i)
    {
        for (size_t j = 0; j < col; ++j)
        {
            std::cout <<  ary[i][j] << " ";
        }
        std::cout << "\n";
    }
    for (size_t i = 0; i < row; ++i )
    {
        delete [] ary[i];
    }
    delete [] ary;
}
/*tested with file:
2 3
1 2 3
4 5 6
*/

one final point:
1
2
3
for (int i=0; i<a; i++)
delete[]matrica[i];
delete[]matrica;

braces around for loop makes it clear how many times delete[] matrica is called - a times or just once?
Last edited on
Topic archived. No new replies allowed.