C++ 从继承类写入.txt文件问题(C+;+;)
基本上,我试图将数据输入(已经由用户输入,只是试图打印)中的输入保存到.txt文件中。这是来自一个主类和一个子类,但它似乎只是在保存子类的详细信息 我简化了示例中的class/int main,因为我的代码太长了 有没有办法将“车辆”和“车辆”数据项一起保存到同一文档中 谢谢C++ 从继承类写入.txt文件问题(C+;+;),c++,inheritance,C++,Inheritance,基本上,我试图将数据输入(已经由用户输入,只是试图打印)中的输入保存到.txt文件中。这是来自一个主类和一个子类,但它似乎只是在保存子类的详细信息 我简化了示例中的class/int main,因为我的代码太长了 有没有办法将“车辆”和“车辆”数据项一起保存到同一文档中 谢谢 class vehicle { public: virtual void saveDetails() { ofstream vehiclefile ("vehicle.txt");
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;
}
class车辆{
公众:
虚拟void saveDetails(){
流车辆文件(“vehicle.txt”);
vehiclefile.open(“vehicle.txt”);
vehiclefile您将希望第二次打开该文件进行追加
在您的卡车类中尝试使用以下旗帜打开:
vehiclefile.open ("vehicle.txt", ios::out | ios::app );
看看:
编辑:我添加了这个作为注释,但为了清楚起见,我也添加了这个答案-哦,另外,您的基类正在进行双重打开。请尝试删除“vehicle.txt”或者不在下一行调用open。除了在子类中使用上述标志外,还可以执行此操作。您将希望第二次打开该文件以进行追加
在您的卡车类中尝试使用以下旗帜打开:
vehiclefile.open ("vehicle.txt", ios::out | ios::app );
看看:
编辑:我添加了这个作为注释,但为了清楚起见,我也添加了这个答案-哦,另外,您的基类正在进行双重打开。请尝试删除“vehicle.txt”从构造函数或不在下一行调用open。除了在子类中使用上述标志外,还可以执行此操作。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();
}
class车辆{
公众:
虚拟void saveDetails(){
流式车辆文件;
vehiclefile.open(“vehicle.txt”,ios::app | ios::out);
vehiclefileOfstream.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();
}
class车辆{
公众:
虚拟void saveDetails(){
流式车辆文件;
vehiclefile.open(“vehicle.txt”,ios::app | ios::out);
vehiclefile@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;
}
#包括
等级车辆
{
公众:
虚拟void saveDetails()
{
int制造商=5;
国际年=1989年;
int regnum=0;
std::of Stream vehiclefile(“vehicle.txt”);
//vehiclefile.open(“vehicle.txt”);//删除此文件
vehiclefile@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;
}
#包括
等级车辆
{
公众:
虚拟void saveDetails()
{
int制造商=5;
国际年=1989年;
int regnum=0;
std::of Stream vehiclefile(“vehicle.txt”);
//vehiclefile.open(“vehicle.txt”);//删除此文件
vehiclefile我在问题中说:)它只保存“lorry”子类的详细信息,跳过父“vehicle”类的详细信息。它是否每次都覆盖该文件?也许你只看到子类信息,因为每次调用ofstream.Open都会覆盖文件vehicle.txt?尝试使用应用程序(追加)或ate(结尾)打开标志我已经尝试过在子类中去掉“vehiclefile.open(“vehicle.txt”);但仍然会发生:\Tryvehiclefile.open(“vehicle.txt”,std::ofstream::out | std::ofstream::app);
它看起来很可能会覆盖所有数据,因为它从文件的开头开始。只是猜测一下,但请尝试从Stream vehiclefile(“vehicle.txt”)的中删除文件名;
在基类中以及使用ios:app。我在问题中说:)它只保存“lorry”子类中的详细信息,跳过父“vehicle”类中的详细信息。它是否每次都覆盖文件?也许你只看到子类信息,因为每次调用ofstream.Open都会覆盖文件vehicle.txt?尝试使用e app(append)或ate(end)open flag我已经尝试过在子类中去掉“vehiclefile.open”(“vehicle.txt”);但仍然会发生:\Tryvehiclefile.open(“vehicle.txt”,std::ofstream::out | std::ofstream::app)
它看起来很可能会覆盖所有数据,因为它从文件的开头开始。只是猜测一下,但请尝试从中删除文件名