【发布时间】:2009-04-02 07:38:16
【问题描述】:
正如你们中的大多数人可能已经知道我的一系列问题一样,我正在尝试创建一个程序,该程序可以将多个结构序列化为 .dat 文件,通过加载序列化将它们读回,编辑内容,然后然后将它们重新写入文件等等。这是我正在尝试做的一个库存计划,但我无法让它终生发挥作用。
我正在加载的文件是空白的。我的程序甚至需要 10 秒才能加载,现在我知道为什么了。这是因为我的向量的大小是 25 万。哦等等……这次我运行它,我的向量大小是 5,172,285。那是一个非常大的充满结构的向量。没有任何运行时或编译错误,但我很确定我做错了什么。我正在加载的文件也是完全空白的。
代码:
// Project 5.cpp : main project file.
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <algorithm>
using namespace System;
using namespace std;
#pragma hdrstop
int checkCommand (string line);
template<typename T>
void writeVector(ofstream &out, const vector<T> &vec);
template<typename T>
vector<T> readVector(ifstream &in);
struct InventoryItem {
string Item;
string Description;
int Quantity;
int wholesaleCost;
int retailCost;
int dateAdded;
} ;
int main(void)
{
cout << "Welcome to the Inventory Manager extreme! [Version 1.0]" << endl;
ifstream in("data.dat");
if (in.is_open()) { cout << "File \'data.dat\' has been opened successfully." << endl; } else { cout << "Error opening data.dat" << endl; return 0;}
cout << "Loading data..." << endl;
vector<InventoryItem> structList = readVector<InventoryItem>( in );
cout <<"Load complete." << endl;
while (1)
{
string line = "";
cout << "There are currently " << structList.size() << " items in memory.";
cout << endl;
cout << "Commands: " << endl;
cout << "1: Add a new record " << endl;
cout << "2: Display a record " << endl;
cout << "3: Edit a current record " << endl;
cout << "4: Exit the program " << endl;
cout << endl;
cout << "Enter a command 1-4: ";
getline(cin , line);
int rValue = checkCommand(line);
if (rValue == 1)
{
cout << "You've entered a invalid command! Try Again." << endl;
} else if (rValue == 2){
cout << "Error calling command!" << endl;
} else if (!rValue) {
break;
}
}
system("pause");
return 0;
}
int checkCommand (string line)
{
int intReturn = atoi(line.c_str());
int status = 3;
switch (intReturn)
{
case 1:
break;
case 2:
break;
case 3:
break;
case 4:
status = 0;
break;
default:
status = 1;
break;
}
return status;
}
template<typename T>
void writeVector(ofstream &out, const vector<T> &vec)
{
out << vec.size();
for(vector<T>::const_iterator i = vec.begin(); i != vec.end(); i++)
{
out << *i;
}
}
ostream &operator<<(ostream &out, const InventoryItem &i)
{
out << i.Item << i.Description;
out << i.Quantity;
out << i.wholesaleCost << i.retailCost;
out << i.dateAdded;
return out;
}
istream &operator>>(istream &in, InventoryItem &i)
{
in >> i.Item >> i.Description;
in >> i.Quantity;
in >> i.wholesaleCost >> i.retailCost;
in >> i.dateAdded;
return in;
}
template<typename T>
vector<T> readVector(ifstream &in)
{
size_t size;
in >> size;
vector<T> vec;
vec.reserve(size);
for(unsigned int i = 0; i < size; i++)
{
T tmp;
in >> tmp;
vec.push_back(tmp);
}
return vec;
}
谁能简单地告诉我如何将其转换为一个程序,该程序实际上可以将充满结构的序列化向量写入文件,然后将它们读回,以便我可以编辑它们并将它们存储回去以供以后加载?哦,我的天哪,这是一次多么愉快的旅程!
感谢您提供的任何帮助!
【问题讨论】:
-
最好自己做作业
-
关于作业主题的问题可以提问,礼仪不是解决问题的全部,只是针对问题给出提示和建议。如果在您尝试学习时根本没有任何帮助,这可能会令人沮丧
标签: c++ serialization file vector struct