从继承类写入.txt文件的问题(C++)

Writing to .txt file from inheritance classes issue (C++)

本文关键字:问题 C++ 文件 txt 继承      更新时间:2023-10-16

基本上,我试图将数据输入(用户已经输入,只是试图打印)保存到.txt文件中。这是来自一个主类和一个子类,但它似乎只是在保存子类的详细信息。。。

由于我的代码太长,我在示例中简化了类/int-main。

有什么想法吗?我该如何将"车辆"answers"汽车"数据条目一起保存到同一文档中?

感谢

class vehicle {    
public:     
virtual void saveDetails() { 
    ofstream vehiclefile ("vehicle.txt");
    vehiclefile.open ("vehicle.txt");
    vehiclefile << "***Your Vehicle's Details***" << endl;
    vehiclefile << "Manufacturer:" << manufacturer << endl;
    vehiclefile << "Year of Manufacture:" << year << endl;
    vehiclefile << "Registration Number: " << regnum << endl;
    vehiclefile.close();
}

class lorry : public vehicle{
public:
  virtual void saveDetails() { 
    vehicle::saveDetails();
    ofstream vehiclefile;
    vehiclefile.open ("vehicle.txt");
    vehiclefile << "Car or Lorry: Lorry" << endl;
    vehiclefile << "Maximum weight: " << tonnage << endl;
    vehiclefile << "Body type: " << bodtype << endl;
    vehiclefile.close();
}

int main (); {
case '3': 
        v -> saveDetails();
        break;
}

Ofstream.open采用模式参数。每次打开文件时,每次连续的写入都会从文件的前面开始。尝试将模式设置为app(append)或ate(在最后,尽管我不太确定这种行为,因为我还没有测试过,正在从手机上发布)

vehiclefile.open("vehicle.txt", ios::out | ios::app);它看起来很可能会覆盖所有数据,因为它从文件的开头开始,而不是追加。

同时从基类中的构造函数中删除文件名,这会导致文件打开,并且没有指定任何标志。。。即使您在调用中使用它们来打开,您也已经在前一行打开了文件。

 class vehicle {    
 public:     
 virtual void saveDetails() { 
ofstream vehiclefile;
vehiclefile.open ("vehicle.txt", ios::app|ios::out);
vehiclefile << "***Your Vehicle's Details***" << endl;
vehiclefile << "Manufacturer:" << manufacturer << endl;
vehiclefile << "Year of Manufacture:" << year << endl;
vehiclefile << "Registration Number: " << regnum << endl;
vehiclefile.close();
} 

http://www.cplusplus.com/reference/fstream/ofstream/open/有一些关于模式的信息。

关于构造函数,来自上面的链接:

由于对文件流执行的第一个任务通常是打开文件,因此这三个类包括一个构造函数,该构造函数自动调用打开成员函数,并且具有与该成员完全相同的参数。

删除多余的文件open并使用append-open标志(在两个clssse中),你应该会很好。

您需要第二次打开该文件进行追加。

试着在你的卡车类中打开这样的旗帜:

vehiclefile.open ("vehicle.txt", ios::out | ios::app );

看看:http://www.cplusplus.com/doc/tutorial/files/

编辑:我添加了这个作为评论,但为了清楚起见,我也添加了这个答案——哦,另外,你的基类正在进行双重开放。尝试从构造函数中删除"vehicle.txt",或者不在下一行调用open。除了在您的子类中使用前面提到的标志之外,还要执行此操作。

@user3495829是对的。您需要从基类中删除open方法,并在派生类中打开时使用append选项。下面是一个将两组数据写入文件的工作示例。我添加了伪值,所以它可以编译。

#include <fstream>
class vehicle
{
public:
    virtual void saveDetails() 
    { 
        int manufacturer = 5;
        int year = 1989;
        int regnum = 0;
        std::ofstream vehiclefile ("vehicle.txt");
        //vehiclefile.open ("vehicle.txt");  // Remove this
        vehiclefile << "***Your Vehicle's Details***" << std::endl;
        vehiclefile << "Manufacturer:" << manufacturer << std::endl;
        vehiclefile << "Year of Manufacture:" << year << std::endl;
        vehiclefile << "Registration Number: " << regnum << std::endl;
        vehiclefile.close();
    }
};

class lorry : public vehicle
{
public:
    virtual void saveDetails() 
    { 
        int tonnage = 1;
        int bodtype = 1;
        vehicle::saveDetails();
        std::ofstream vehiclefile;
        vehiclefile.open ("vehicle.txt", std::ios::out | std::ios::app); // Append
        vehiclefile << "Car or Lorry: Lorry" << std::endl;
        vehiclefile << "Maximum weight: " << tonnage << std::endl;
        vehiclefile << "Body type: " << bodtype << std::endl;
        vehiclefile.close();
    }
};
int main ()
{
    vehicle* v = new lorry();
    v -> saveDetails();
    delete v;
    return 0;
}