One*_*hot 1 c++ serialization struct file vector
由于你们大多数人可能都在关注我已经知道的问题,我正在尝试创建一个程序,可以将多个结构序列化为.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;
}
Run Code Online (Sandbox Code Playgroud)
有人可以简单地向我展示如何将其转换为一个程序,该程序实际上可以将完整结构的序列化向量写入文件,然后将它们读回来以便我可以编辑它们并将它们存储回来以便以后加载吗?噢,天哪,这是多么的好!
感谢您提供任何帮助!
你说文件实际上是空的.readVector的第一行是这样的:
in >> size;
Run Code Online (Sandbox Code Playgroud)
你认为实际上最终的大小是什么?由于它是空的,这将导致您没有检测到的错误.变量size将保持未初始化状态 - 因此您会在其中看到奇怪的值,因为它会占用当时在内存中发生的任何值.您可以使用以下检查来检查流的状态:
if (in)
Run Code Online (Sandbox Code Playgroud)
在布尔上下文中处理它会告诉您是否发生了错误 - 这也将涵盖无法读取有效整数变量的内容.我建议您应该弄清楚如何在调试器中运行程序,您将能够逐步执行代码并查看在给定时间变量的值.