I'm trying to write a programme that opens a file and uses it to make a table which is called "plansza" in our part of code we have to define a constructor
Plansza::Plansza(char * Nameoffile)
but how can we later use our name of file to actually open it?? In int main() should it be something like
Plansza plansza("text.txt")
But that's not working..
Besides this we also want to be able to use that table later, like changing some numbers or sth like that.. So we have to know where there is . and where 0
Our example file text.txt looks like this
0......0
...0....
...0....
...0....
0......0
and here is our code..
class Plansza{
private:
int *ekran;
int szer;
int wys;
public:
Plansza();
Plansza(int m,int n);
Plansza(const Plansza &plansza);
~Plansza();
Plansza& operator=(const Plansza& plansza);
Plansza(char* NazwaPliku);
void Wyswietl();
int Szerokosc();
int Wysokosc();
void Set(int x,int y,int k);
int Get(int x,int y);
};
Plansza::Plansza(){
ekran=new int[12];
szer=4;
wys=3;
for (int i=0;i<szer*wys+1;i++)
ekran[i]=0;
}
Plansza::Plansza(const Plansza &plansza){
wys=plansza.wys;
szer=plansza.szer;
ekran=new int[wys*szer];
for (int i=0;i<wys*szer+1;++i)
ekran[i]=plansza.ekran[i];
}
Plansza::~Plansza(){
delete []ekran;
}
Plansza& Plansza::operator=(const Plansza& plansza){
if (&plansza==this)
return *this;
if (ekran != NULL)
delete []ekran;
szer=plansza.szer;
wys=plansza.wys;
ekran=new int[szer*wys];
for (int i=0; i<szer*wys+1; i++)
ekran[i]=plansza.ekran[i];
return *this;
}
Plansza::Plansza(int m,int n){
ekran=new int[m*n];
szer=n;
wys=m;
for (int i=0;i<m*n+1;i++)
ekran[i]=0;
}
Plansza::Plansza(char* NazwaPliku){
FILE * plik;
plik= fopen("Test.txt", "r");
fclose(plik);
};
void Plansza::Wyswietl(){
for (int i=1;i<szer*wys+1;i++)
{
if (i%(szer)==0){
if (ekran[i]==0)
cout<< "." <<"";
else
cout<< "X" <<"";
cout<< "\n" <<"";
}
else
{
if (ekran[i]==0)
cout<< "." <<"";
else
cout<< "X" <<"";
}
}
};
int Plansza::Szerokosc(){
return szer;
};
int Plansza::Wysokosc(){
return wys;
};
void Plansza::Set(int x,int y,int k){
if ((x>wys) || (y>szer) || (x<1) || (y<1))
cout<<"Jestes poza tablica"<<"\n";
else
ekran[(x-1)*szer+y]=k;
};
int Plansza::Get(int x,int y){
if ((x<=wys) && (y<=szer) && (x>=1) && (y>=1))
return ekran[(x-1)*szer+y];
else
{
cout<<"Jestes poza tablica"<<"\n";
return 0;
}
};