Primitive file write and read

This commit is contained in:
Inex Code 2020-09-07 09:07:27 +00:00
parent 56b38af479
commit eeb0cdd9f0
1 changed files with 69 additions and 29 deletions

View File

@ -35,7 +35,7 @@ public:
} }
} }
void freemem() void freemem()
{ {
free(data); free(data);
} }
@ -66,24 +66,26 @@ public:
int get(uint x, uint y) int get(uint x, uint y)
{ {
// TODO реализовать логику // TODO реализовать логику
return data[mx * (y) + (x)]; return data[mx * (y) + (x)];
} }
void set(uint x, uint y, uint color) void set(uint x, uint y, uint color)
{ {
if (x >= mx) { if (x >= mx)
{
throw(x); throw(x);
} }
if (y >= my) { if (y >= my)
{
throw(y); throw(y);
} }
data[mx * (y) + (x)] = color; data[mx * (y) + (x)] = color;
// TODO реализовать логику // TODO реализовать логику
} }
void copy(Image* original) void copy(Image *original)
{ {
freemem(); freemem();
mx = original->getMx(); mx = original->getMx();
@ -99,7 +101,7 @@ public:
} }
// Returns true if equal // Returns true if equal
bool compare(Image* original) bool compare(Image *original)
{ {
if (original->getMx() == mx && original->getMy() == my) if (original->getMx() == mx && original->getMy() == my)
{ {
@ -107,7 +109,8 @@ public:
{ {
for (uint x = 0; x < mx; x++) for (uint x = 0; x < mx; x++)
{ {
if (data[mx * y + x] != original->get(x, y)){ if (data[mx * y + x] != original->get(x, y))
{
cout << "Not equal: x = " << x << "; y = " << y << "\n"; cout << "Not equal: x = " << x << "; y = " << y << "\n";
return false; return false;
} }
@ -123,58 +126,90 @@ public:
} }
} }
void draw_horizontal(uint y, uint color) { void draw_horizontal(uint y, uint color)
if (y >= my) { {
if (y >= my)
{
throw(y); throw(y);
} }
for (uint x = 0; x < mx; x++) { for (uint x = 0; x < mx; x++)
data[mx*y + x] = color; {
data[mx * y + x] = color;
} }
} }
void draw_vertical(uint x, uint color) { void draw_vertical(uint x, uint color)
if (x >= mx) { {
if (x >= mx)
{
throw(x); throw(x);
} }
for (uint y = 0; y < my; y++) { for (uint y = 0; y < my; y++)
data[mx*y + x] = color; {
data[mx * y + x] = color;
} }
} }
void save_to_file(string filename) { void save_to_file(string filename)
{
ofstream writefile; ofstream writefile;
writefile.open(filename); writefile.open(filename);
if (writefile.is_open()){ if (writefile.is_open())
writefile << mx << "\t" << my << "\n"; {
writefile << mx << "\n"
<< my << "\n";
for (uint y = 0; y < my; y++) for (uint y = 0; y < my; y++)
{ {
for (uint x = 0; x < mx; x++) for (uint x = 0; x < mx; x++)
{ {
writefile << data[mx * y + x] << "\t"; writefile << data[mx * y + x] << "\n";
} }
writefile << "\n";
} }
writefile << "\n";
writefile.close(); writefile.close();
} else }
else
{ {
cerr << "saving to file failed\n"; cerr << "saving to file failed\n";
} }
} }
void read_from_file(string filename) { void read_from_file(string filename)
{
string line; string line;
ifstream readfile; ifstream readfile;
readfile.open(filename); readfile.open(filename);
if(readfile.is_open()) { if (readfile.is_open())
{
// TODO // TODO
try
{
getline(readfile, line);
mx = stoi(line);
} else { getline(readfile, line);
my = stoi(line);
for (uint y = 0; y < my; y++)
{
for (uint x = 0; x < mx; x++)
{
getline(readfile, line);
data[mx * y + x] = stoi(line);
}
}
}
catch (const exception &e)
{
cerr << e.what() << '\n';
}
}
else
{
cerr << "reading file failed"; cerr << "reading file failed";
} }
readfile.close(); readfile.close();
} }
}; };
Image a(30, 10); Image a(30, 10);
@ -187,13 +222,15 @@ int main()
cout << "Image b \n"; cout << "Image b \n";
b.show(); b.show();
b.copy(&a); b.copy(&a);
if (b.compare(&a) == true) { if (b.compare(&a) == true)
{
cout << "a == b after copy\n"; cout << "a == b after copy\n";
} else }
else
{ {
cout << "a != b after copy!\n"; cout << "a != b after copy!\n";
} }
b.set(4, 3, 20); b.set(4, 3, 20);
for (int i = 5; i < 9; i++) for (int i = 5; i < 9; i++)
{ {
@ -207,5 +244,8 @@ int main()
cout << "Image b, with a horizontal line \n"; cout << "Image b, with a horizontal line \n";
b.show(); b.show();
b.save_to_file("image"); b.save_to_file("image");
a.read_from_file("image");
cout << "Image a, read from file \n";
a.show();
return 0; return 0;
} }