Member 13338695 Ответов: 1

Как сохранить векторный объект ob в файл [C++]


#include "stdafx.h"
#include <conio.h>
#include <iostream>
#include <string>
#include <stdlib.h>
#include <vector>
#include <fstream>
#include <iterator>
#include <algorithm>

using namespace std;




class Medicine{
private:
	string productName;
	double productPrice;

public:
	Medicine(string productName, double productPrice){
		this->productName = productName;
		this->productPrice = productPrice;
	}

	void printMedicineDetails(){
		cout << "Product Name: " << this->productName << "\n"
			<< "Product Price: " << this->productPrice << "\n";

	}

	// Getter and setter
	string getProductName(){
		return productName;
	}

	void setProductName(string productName){
		productName = productName;
	}
};


//Function Prototype
void addMedicine(vector<Medicine>& medicineCollection);
void printAllMedicine(vector<Medicine>& medicineCollection);
void deleteMedicine(vector<Medicine>& medicineCollection);
void searchMedicine(vector<Medicine>& medicineCollection);

vector<Medicine> readMedicineRecordFromFile(string filename);
void writeMedicineRecordToFile(vector<Medicine>& medicineCollection, string filename);








int main()
{
	vector <Medicine> medicineCollection;
	string MedicineRecordFilename = "MedicineRecord.txt";
	int menuOption;


	do
	{
		system("CLS");
		cout << "1. Add Medicine" << "\n";
		cout << "2. Delete Medicine" << "\n";
		cout << "3. Modify Medicine" << "\n";
		cout << "4. Search Medicine" << "\n";
		cout << "5. Medicine Expiry Date" << "\n";
		cout << "6. Return to Login menu " << "\n";
		cout << "7. Exit " << "\n\n";



		cout << "Enter option: ";
		cin >> menuOption;

		switch (menuOption) {

		case 1:
			addMedicine(medicineCollection);
			system("PAUSE");
			break;
		case 2:
			deleteMedicine(medicineCollection);
			system("PAUSE");
			break;
		case 3:
			printAllMedicine(medicineCollection);
			system("PAUSE");
			break;
		case 4:
			searchMedicine(medicineCollection);
			system("PAUSE");
			break;
		case 7:
			cout << "System Terminal succesfully" << "\n";
			system("PAUSE");

			system(0);


		default:
			cout << "Invalid Input, Please try again." << "\n";
			break;
		}

	} while (menuOption != 7);

	getchar();
	return 0;
}

void addMedicine(vector<Medicine>& medicineCollection) {
	string productName;
	double productPrice;

	cout << "Enter Product Name: ";
	cin >> productName;

	cout << "Enter Product Price: ";
	cin >> productPrice;

	Medicine newMedicine(productName, productPrice);
	medicineCollection.push_back(newMedicine);

	cout << "Medicine succesfully recorded.";


}

void printAllMedicine(vector<Medicine>& medicineCollection) {
	if (medicineCollection.empty()){
		cout << "No, Medicine record yet." << "\n";

		return;
	}

	for (Medicine medicine : medicineCollection) {
		cout << medicine.getProductName() << "\n";
	}
}

void deleteMedicine(vector<Medicine>& medicineCollection) {

	string productName;

	if (medicineCollection.empty()) {
		cout << "No, Medicine record yet." << "\n";

		return;
	}

	cout << "Enter Product Name: ";
	cin >> productName;

	for (unsigned int i = 0; i < medicineCollection.size(); i++) {

		if (productName.compare(medicineCollection[i].getProductName()) == 0) {

			medicineCollection.erase(medicineCollection.begin() + i);
			cout << "Medicine record has succesfully deleted.";
			return;

		}

		cout << "Sorry Medicine record not found.";
	}


}

void searchMedicine(vector<Medicine>& medicineCollection) {

	string productName;

	if (medicineCollection.empty()) {
		cout << "No, Medicine record yet." << "\n";

		return;
	}

	cout << "Enter Product Name: ";
	cin >> productName;

	for (unsigned int i = 0; i < medicineCollection.size(); i++) {

		if (productName.compare(medicineCollection[i].getProductName()) == 0) {

			medicineCollection[i].printMedicineDetails();
			return;

		}

		cout << "Sorry Medicine record not found.";
	}
}


Что я уже пробовал:

Я пытаюсь сохранить вектор лекарства в файл, но это не работает, пожалуйста, помогите мне?!

std::ofstream outFile;
outFile.open(fileName, ios::out);
for(int i=0;i<obj.size();i++)
    {
        outFile.write((const char *)(obj.data()),sizeof(vector<vector<Medicine> >)*obj.size());
    }
outFile.close();

Patrice T

"но это не работает" не является информативным, пожалуйста, определите.

1 Ответов

Рейтинг:
2

CPallini

Взгляните на Сериализация[^]. Я предлагаю вам добавить поддержку сериализации в ваш Medicine класс (например, методы load/save), а затем итерация по векторным элементам для сериализации всего контейнера.