c++:定义一个Dog类,包含体重和年龄两个成员变量及相应的成员函数。声明一个实例dog1,体重为5,年龄为10,使用I/O流把dog1的状态写入磁盘文件。再声明另一个实例dog2,通过读文件把dog

你可以创建一个Dog对象,并设置其weight和age属性。然后,使用writeToFileText方法将其状态写入一个文本文件,使用writeToFileBinary方法将其状态写入一个二进制文件。接下来,你可以创建另一个Dog对象,并使用readFromFileText方法从文本文件中读取其状态,然后使用readFromFileBinary方法从二进制文件中读取其状态。最后,你可以输出第二个Dog对象的属性,以验证读取操作是否成功。

.h文件


/**
定义一个Dog类,包含体重和年龄两个成员变量及相应的成员函数。声明一个实例dog1,体重为5,年龄为10,
使用I/O流把dog1的状态写入磁盘文件。再声明另一个实例dog2,通过读文件把dog1的状态赋给dog2。分别使用文本文件和二进制方式操作文件。

 */
#ifndef INC_0908C_DOG2_H
#define INC_0908C_DOG2_H
#include <iostream>
#include <fstream>
using namespace std;

class Dog2 {
private:
    double weight;
    int age;
public:
    Dog2();
    Dog2(double weight,int age);
   void setWeight(double weight);
   void setAge(int age);
   double getWeight() const;
    int getAge() const;
    void writeToFileText(const std::string&filename)const;  //将对象以文本形式写入文件
    void readFromFileText(const std::string&filename);//从文本文件中读取对象状态
    void writeToFileBinary(const std::string&filename)const; //将对象状态以二进制形式写入文件
    void readFromFileBinary(const std::string&filename);    //从二进制文件中读取对象状态
};


#endif //INC_0908C_DOG2_H

.cpp文件



#include "Dog2.h"

double Dog2::getWeight() const {
    return weight;
}

int Dog2::getAge() const {
    return age;
}

Dog2::Dog2():weight(0.0),age(0) {

}

Dog2::Dog2(double weight, int age):weight(weight),age(age) {

}

void Dog2::setWeight(double weight) {
    this->weight=weight;
}

void Dog2::setAge(int age) {
    this->age=age;
}

void Dog2::writeToFileText(const string &filename) const {
    std::ofstream outputFile(filename);
    if(outputFile.is_open()){
        outputFile<<weight<<"\n";
        outputFile<<age<<"\n";
        outputFile.close();
        std::cout<<"成功讲狗的状态写入文本文件:"<<filename<<std::endl;
    }else{
        std::cerr<<"打开文本文件时出错:"<<std::endl;
    }
}

void Dog2::readFromFileText(const string &filename) {
        std::ifstream inputFile(filename);
    if (inputFile.is_open()){
        inputFile>>weight;
        inputFile>>age;
        inputFile.close();
        std::cout<<"成功从文本文件中读取狗的状态:"<<filename<<std::endl;
    }else{
        std::cerr<<"打开文本文件时出错:"<<std::endl;
    }
}

void Dog2::writeToFileBinary(const string &filename) const {
    std::ofstream  outputFile(filename,std::ios::binary);
    if (outputFile.is_open()){
        outputFile.write(reinterpret_cast<const char*>(&weight),sizeof (weight));
        outputFile.write(reinterpret_cast<const char*>(&age),sizeof (age));
        outputFile.close();
        std::cout<<"成功将狗的状态写入二进制文件:"<<filename<<std::endl;
    }else{
        std::cerr<<"打开二进制文件时出错:"<<std::endl;
    }
}

void Dog2::readFromFileBinary(const std::string &filename) {
    std::ifstream inputFile(filename,std::ios::binary);
    if(inputFile.is_open()){
        inputFile.read(reinterpret_cast<char*>(&weight),sizeof (weight));
        inputFile.read(reinterpret_cast<char*>(&age),sizeof (age));
        inputFile.close();
        std::cout<<"成功将狗的状态写入二进制文件:"<<filename<<std::endl;
    }else{
        std::cerr<<"打开二进制文件时出错:"<<std::endl;
    }
}

main.c文件


#include "Dog2.h"
int main() {
    Dog2 dog1(5.0, 10);
    //将dog1写入文本文件
    dog1.writeToFileText(R"(C:\Users\86157\Desktop\dog1.txt)");

    // //将dog1的状态写入二进制文件
    dog1.writeToFileBinary(R"(C:\Users\86157\Desktop\dog1.bin)");

    Dog2 dog2;

    //从文本文件中读取dog1的状态并将其分配给dog2
    dog2.readFromFileText(R"(C:\Users\86157\Desktop\dog1.txt)");
    std::cout << "dog2的状态(从文本文件中获取:)" << std::endl;
    std::cout << "体重:" << dog2.getWeight() << std::endl;
    std::cout << "年龄" << dog2.getAge() << std::endl;
    std::cout << std::endl;

    // 从二进制文件中读取dog1的状态并将其分配给dog2
    dog2.readFromFileBinary(R"(C:\Users\86157\Desktop\dog1.bin)");
    std::cout << "dog2的状态(从二进制文件中获取:)" << std::endl;
    std::cout << "体重:" << dog2.getWeight() << std::endl;
    std::cout << "年龄" << dog2.getAge() << std::endl;
}

在main函数中创建Dog类的实例,并测试其读写文件的功能。

确保将上述三个文件放在同一个目录中,并使用C++编译器编译并运行main.cpp文件,以测试Dog类的功能。记得在编译时链接所有必要的源文件。

注意:上述代码只提供了基本的文件读写操作,你可以根据需要进行扩展和修改。

这是一个简单的C++示例,演示了如何将一个`Dog`对象的状态以文本和二进制形式写入文件,并从文件中读取对象的状态。 在这个示例中,`Dog`类具有私有成员变量`weight`(体重)和`age`(年龄),以及公共成员函数来设置和获取这些变量的值。`Dog`类还有用于将对象状态写入文件和从文件中读取对象状态的成员函数。 在`writeToFileText`函数中,我们使用`std::ofstream`来打开一个文本文件,并将`weight`和`age`的值写入文件。在`readFromFileText`函数中,我们使用`std::ifstream`来打开相同的文本文件,并从文件中读取`weight`和`age`的值。 类似地,在`writeToFileBinary`函数中,我们使用`std::ofstream`以二进制模式打开文件,并将`weight`和`age`的值作为二进制数据写入文件。在`readFromFileBinary`函数中,我们使用`std::ifstream`以二进制模式打开相同的文件,并从文件中读取二进制数据,并将其解释为`weight`和`age`的值。 在`main`函数中,我们创建了一个名为`dog1`的`Dog`对象,并将其状态写入名为`dog1.txt`和`dog1.bin`的文本和二进制文件中。然后,我们创建了一个名为`dog2`的空`Dog`对象,并从文本文件和二进制文件中读取`dog1`的状态,并将其赋值给`dog2`。最后,我们输出了`dog2`的状态。 请注意,这只是一个简单的示例,目的是演示文件的读写操作。在实际的程序中,可能需要处理错误、异常情况以及更复杂的对象状态。

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值