[英]Writing to .txt file from inheritance classes issue (C++)
基本上,我试图将来自数据条目(已经由用户输入,只是试图打印)的输入保存到.txt文件。 这是来自主类和子类的,但似乎只是在保存子类的详细信息...
由于代码太长,我在示例中简化了class / int main。
有什么想法可以将“车辆”和“汽车”数据条目一起保存到同一文档中吗?
谢谢
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;
}
您需要第二次打开文件以进行追加。
在您的货车类中尝试使用以下标志打开:
vehiclefile.open ("vehicle.txt", ios::out | ios::app );
看看: http : //www.cplusplus.com/doc/tutorial/files/
编辑:我添加了此作为注释,但为清楚起见,我也添加了此答案-哦,另外,您的基类正在进行两次打开。 尝试从构造函数中删除“ vehicle.txt”,或者不调用下一行的open。 除了在子类中使用上述标志之外,还请执行此操作。
Ofstream.open采用模式参数。 每次打开文件时,似乎每次连续写入都从文件的开头开始。 尝试将模式设置为“应用”(添加)或“吃”(尽管我不确定此行为,因为我尚未测试并且正在通过手机发布)
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标志(在两个clsss中都可以),您应该会很好。
@ 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;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.