Primitive file write and read

This commit is contained in:
Inex Code 2020-09-07 09:07:27 +00:00
parent 56b38af479
commit eeb0cdd9f0

View file

@ -73,17 +73,19 @@ public:
void set(uint x, uint y, uint color)
{
if (x >= mx) {
if (x >= mx)
{
throw(x);
}
if (y >= my) {
if (y >= my)
{
throw(y);
}
data[mx * (y) + (x)] = color;
// TODO реализовать логику
}
void copy(Image* original)
void copy(Image *original)
{
freemem();
mx = original->getMx();
@ -99,7 +101,7 @@ public:
}
// Returns true if equal
bool compare(Image* original)
bool compare(Image *original)
{
if (original->getMx() == mx && original->getMy() == my)
{
@ -107,7 +109,8 @@ public:
{
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";
return false;
}
@ -123,58 +126,90 @@ public:
}
}
void draw_horizontal(uint y, uint color) {
if (y >= my) {
void draw_horizontal(uint y, uint color)
{
if (y >= my)
{
throw(y);
}
for (uint x = 0; x < mx; x++) {
data[mx*y + x] = color;
for (uint x = 0; x < mx; x++)
{
data[mx * y + x] = color;
}
}
void draw_vertical(uint x, uint color) {
if (x >= mx) {
void draw_vertical(uint x, uint color)
{
if (x >= mx)
{
throw(x);
}
for (uint y = 0; y < my; y++) {
data[mx*y + x] = color;
for (uint y = 0; y < my; y++)
{
data[mx * y + x] = color;
}
}
void save_to_file(string filename) {
void save_to_file(string filename)
{
ofstream writefile;
writefile.open(filename);
if (writefile.is_open()){
writefile << mx << "\t" << my << "\n";
if (writefile.is_open())
{
writefile << mx << "\n"
<< my << "\n";
for (uint y = 0; y < my; y++)
{
for (uint x = 0; x < mx; x++)
{
writefile << data[mx * y + x] << "\t";
writefile << data[mx * y + x] << "\n";
}
}
writefile << "\n";
}
writefile.close();
} else
}
else
{
cerr << "saving to file failed\n";
}
}
void read_from_file(string filename) {
void read_from_file(string filename)
{
string line;
ifstream readfile;
readfile.open(filename);
if(readfile.is_open()) {
if (readfile.is_open())
{
// 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";
}
readfile.close();
}
};
Image a(30, 10);
@ -187,9 +222,11 @@ int main()
cout << "Image b \n";
b.show();
b.copy(&a);
if (b.compare(&a) == true) {
if (b.compare(&a) == true)
{
cout << "a == b after copy\n";
} else
}
else
{
cout << "a != b after copy!\n";
}
@ -207,5 +244,8 @@ int main()
cout << "Image b, with a horizontal line \n";
b.show();
b.save_to_file("image");
a.read_from_file("image");
cout << "Image a, read from file \n";
a.show();
return 0;
}