简体   繁体   English

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

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

Basically I am trying to save inputs from a data entry (Already been inputted by the user, just trying to print) to a .txt file. 基本上,我试图将来自数据条目(已经由用户输入,只是试图打印)的输入保存到.txt文件。 This is from a main class, and a child class, but it seems to just be saving the child classes's details... 这是来自主类和子类的,但似乎只是在保存子类的详细信息...

I simplified the classes/int main in my example as my code is too long. 由于代码太长,我在示例中简化了class / int main。

Any ideas how can I get it to save the 'vehicle' AND 'car' data entries in to the same document together? 有什么想法可以将“车辆”和“汽车”数据条目一起保存到同一文档中吗?

Thanks 谢谢

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;
}

You'll want to open the file the second time for append. 您需要第二次打开文件以进行追加。

Try open with flags like this in your lorry class: 在您的货车类中尝试使用以下标志打开:

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

Have a look at: http://www.cplusplus.com/doc/tutorial/files/ 看看: http : //www.cplusplus.com/doc/tutorial/files/

EDIT: I added this as a comment, but for clarity I'm adding to this answer too - Oh, additionally your base class is doing a double open. 编辑:我添加了此作为注释,但为清楚起见,我也添加了此答案-哦,另外,您的基类正在进行两次打开。 Try either removing the "vehicle.txt" from the constructor OR not calling the open on the next line. 尝试从构造函数中删除“ vehicle.txt”,或者不调用下一行的open。 Do this in addition to using the aforementioned flags in your child class. 除了在子类中使用上述标志之外,还请执行此操作。

Ofstream.open takes a mode parameter. Ofstream.open采用模式参数。 It looks like each successive write starts at the front of the file each time you open it. 每次打开文件时,似乎每次连续写入都从文件的开头开始。 Try setting the mode to app (append) or ate (at end-though I'm not exactly certain on this behavior because I haven't tested and am posting from my phone) 尝试将模式设置为“应用”(添加)或“吃”(尽管我不确定此行为,因为我尚未测试并且正在通过手机发布)

vehiclefile.open("vehicle.txt", ios::out | ios::app); It looks like it's most likely overwriting all data because it starts at the beginning of the file and is not appending. 看起来它很可能会覆盖所有数据,因为它始于文件的开头而不是追加。

Remove the file name from the constructor in the base class as well, that's causing the file to open and there are no flags specified there... Even if you use them in the call to open you've already opened the file on the previous line. 还要从基类的构造函数中删除文件名,这将导致文件打开,并且没有在其中指定标志...即使您在打开调用中使用它们,您也已经在前一个文件中打开了文件线。

 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/ has some info about the modes. http://www.cplusplus.com/reference/fstream/ofstream/open/具有有关这些模式的一些信息。

On the constructor, from link above: 在构造函数中,从上面的链接:

Since the first task that is performed on a file stream is generally to open a file, these three classes include a constructor that automatically calls the open member function and has the exact same parameters as this member. 由于对文件流执行的第一个任务通常是打开文件,因此这三个类包括一个构造函数,构造函数自动调用open成员函数,并具有与该成员完全相同的参数。

Remove the redundant file open and use the append open flag (in both clssses) and you should be good. 删除打开的冗余文件,并使用append open标志(在两个clsss中都可以),您应该会很好。

@user3495829 is right on the money. @ user3495829是对的钱。 You need to remove the open method from the base class and use the append option when you open in the derived class. 您需要从基类中删除open方法,并在派生类中打开时使用append选项。 Below is a working example that writes both sets of data to the file. 下面是一个将两个数据集都写入文件的工作示例。 I've added dummy values so it compiles. 我添加了虚拟值,以便进行编译。

#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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM