Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <fstream>
- class Answer
- {
- public:
- //конструктор по умолчанию
- Answer()
- {
- x = y = z = 0;
- }
- //перегрузка конструктора
- Answer(int x, int y, int z)
- {
- this->x = x;
- this->y = y;
- this->z = z;
- }
- void Print()
- {
- std::cout << "x - " << x << "\ny - " << y << "\ny - " << y << std::endl;
- }
- int x, y, z;
- };
- void Write(std::string path, Answer &ans)
- {
- std::ofstream fs;
- fs.open(path, std::ofstream::app);
- if(fs.is_open())
- {
- std::cout << "Файл открыт!" << std::endl;
- fs.write(/*приведение ссылки на объект к указателю на char*/(char*)&ans, /*размер элементов*/sizeof(Answer));
- }
- fs.close();
- }
- int main(int argc, char *argv[])
- {
- setlocale(LC_ALL, "Rus");
- srand(time(NULL));
- Answer ans(5, 10, 191);
- Write("file.txt", ans);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement