[英]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.