我做错了我把序列化一个向量与结构到它的.dat文件? [英] What am I doing wrong with my serializing a vector with structs in it to a .dat file?
本文介绍了我做错了我把序列化一个向量与结构到它的.dat文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如果我输入
说明:Apple
数量:10
批发费用:30
零售费用:20
添加日期:12月
这是我的.dat档案中的内容:
1Apple103020December p>
但是当我加载我的程序时,它没有正确加载结构,导致我的列表中有0项。
代码:
#includestdafx.h
#include< iostream>
#include< fstream>
#include< string>
#include< vector>
使用命名空间System;
using namespace std;
#pragma hdrstop
bool isValidChoice(int size,int choice);
template< typename T>
void writeVector(ofstream& out,const vector< T>& vec);
template< typename T>
vector< T> readVector(ifstream& in);
template< typename T>
vector< T> addItem(vector< T>& vec);
template< typename T>
void printItemDescriptions(vector< T& vec);
template< typename T>
int displayRecord(vector< T& vec);
struct InventoryItem {
string描述;
int数量;
int wholesaleCost;
int retailCost;
string dateAdded;
};
int main(void)
{
cout< 欢迎来到库存管理器极限![1.0版]< endl;
ifstream in(data.dat);
if(in.is_open()){cout< 文件\'data.dat\'已成功打开。 << endl; } else {cout<< 打开data.dat时出错< endl;}
cout<< 加载数据...< endl;
vector< InventoryItem> structList = readVector< InventoryItem>(in);
cout<<加载完成。 << endl<< endl;
in.close();
while(1)
{
string line =;
cout<< 目前有< structList.size()<< items in memory。;
cout<< endl<< endl;
cout<< 命令:< endl;
cout<< 1:添加新记录< endl;
cout<< 2:显示记录< endl;
cout<< 3:编辑当前记录< endl;
cout<< 4:删除记录< endl;
cout<< 5:保存当前信息< endl;
cout<< 6:退出程序< endl;
cout<< endl;
cout<< 输入命令1-6:;
getline(cin,line);
int rValue = atoi(line.c_str());
system(cls);
ofstream out(data.dat);
switch(rValue)
{
case 1:
addItem(structList);
break;
case 2:
displayRecord(structList);
break;
case 3:
break;
case 4:
break;
case 5:
if(!structList.size()){cout< 没有要保存的项目!先输入一个! << endl<< endl;系统(暂停);系统(cls);打破; }
writeVector(out,structList);
break;
case 6:
return 0;
default:
cout<< 命令无效,您只能输入命令号1 - 6.再试一次。<< endl;
}
out.close();
}
系统(pause);
return 0;
}
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< *一世;
}
cout<< 保存完成! << endl<< endl;
}
ostream& operator<<(ostream& out,const InventoryItem& i)
{
out< i。描述;
out<< i.Quantity;
out<< i.wholesaleCost<< i.retailCost;
out<< i.dateAdded;
return out;
}
istream& operator>>(istream& in,InventoryItem& i)
{
在> i。描述;
在>> i.Quantity;
在>> i.wholesaleCost>> i.retailCost;
在>> i.dateAdded;
return in;
}
template< typename T>
vector< T> readVector(ifstream& in)
{
size_t size;
if(in.fail())
{
在>>尺寸;
} else {
size = 0;
}
矢量< T> vec;
vec.reserve(size);
for(unsigned int i = 0; i {
T tmp;
在>> tmp;
vec.push_back(tmp);
}
return vec;
}
template< typename T>
vector< T> addItem(vector< T>& vec)
{
system(cls);
string word;
unsigned int number;
InventoryItem newItem;
cout<< - 添加新项目 - < endl<< endl;
cout<< 输入项目的说明:;
getline(cin,word);
newItem.Description = word;
cout<< endl;
cout<< 输入料品的现有数量:;
getline(cin,word);
number = atoi(word.c_str());
newItem.Quantity = number;
cout<< endl;
cout<< 输入料品的零售成本:;
getline(cin,word);
number = atoi(word.c_str());
newItem.retailCost = number;
cout<< endl;
cout<< 输入料品的批发成本:;
getline(cin,word);
number = atoi(word.c_str());
newItem.wholesaleCost = number;
cout<< endl;
cout<< 输入当前日期:;
getline(cin,word);
newItem.dateAdded = word;
vec.push_back(newItem);
return vec;
}
template< typename T>
void printItemDescriptions(vector< T& vec)
{
int size = vec.size();
if(size)
{
cout<< ---------------------------------<< endl;
cout<< |〜项目描述〜| << endl;
cout<< ---------------------------------<< endl;
cout<< *********************************< endl;
for(int i = 0; i {
cout< (<< i + 1<<)< :<< vec [i] .Description< endl;
}
cout<< *********************************< endl<< endl;
}
}
template< typename T>
int displayRecord(vector< T& vec)
{
string word =;
string quit =quit;
int choice = 1;
int size = vec.size();
if(size)
{
printItemDescriptions(vec);
cout<< endl;
while(1)
{
cout< 键入\exit\返回主菜单。 << endl<< endl;
cout<< 输入\list\重新显示项目。 << endl<< endl;
cout<< endl;
cout<< 选择您要显示的项目的编号:;
getline(cin,word);
if(convertToLower(word)==exit){system(cls); return 0; }
if(convertToLower(word)==list){system(cls); displayRecord(vec); }
choice = atoi(word.c_str());
if(isValidChoice(size,choice))
{
system(cls);
cout<< endl<< [Item(<<< choice<<)details]< endl<< endl;
cout<< ******************< endl;
cout<< * Description *<< vec [choice-1]。描述< endl;
cout<< ******************< endl<< endl;
cout<< ******************< endl;
cout<< *手头数量*<< vec [choice-1]。数量< endl;
cout<< ******************< endl<< endl;
cout<< ******************< endl;
cout<< *批发成本*< vec [choice-1] .wholesaleCost< endl;
cout<< ******************< endl<< endl;
cout<< ******************< endl;
cout<< *零售成本*< vec [choice-1] .retailCost<< endl;
cout<< ******************< endl<< endl;
cout<< ******************< endl;
cout<< *数据添加*< vec [choice-1] .dateAdded<< endl;
cout<< ******************< endl<< endl;
} else {system(cls); cout < 那个项目不存在! << endl; cout < 选择另一个项目或输入\list\以查看可用项目。 << endl<< endl; }
}
} else {cout< 目前没有要显示的项目。 << endl<< endl;系统(暂停);系统(cls); return 0; }
return 1;
}
bool isValidChoice(int size,int choice)
{
for(int i = 0; i <= size; i ++)
{
if(choice == size){return true; }
}
return false;
}
string convertToLower(string word)
{
for(unsigned int i = 0; i< word.size(); i ++)
{
word [i] = tolower(word [i]);
}
return word;
}
解决方案
/ p>
if(in.fail())
应为:
if(!in.fail
或更好:
int n;
if(!(in>> n)){
n = 0;
}
If I type in
Description: Apple
Quantity: 10
Wholesale Cost: 30
Retail Cost: 20
Date Added: December
These are the contents in my .dat file:
1Apple103020December
But when I load my program, it doesn't load the struct back in correctly resulting in there being 0 items in my list. Is that what it is suppose to look like or am I doing something seriously wrong.
Code:
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
using namespace System;
using namespace std;
#pragma hdrstop
bool isValidChoice(int size, int choice);
template<typename T>
void writeVector(ofstream &out, const vector<T> &vec);
template<typename T>
vector<T> readVector(ifstream &in);
template<typename T>
vector<T> addItem(vector<T> &vec);
template<typename T>
void printItemDescriptions(vector<T> &vec);
template<typename T>
int displayRecord(vector<T> &vec);
struct InventoryItem {
string Description;
int Quantity;
int wholesaleCost;
int retailCost;
string 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;}
cout << "Loading data..." << endl;
vector<InventoryItem> structList = readVector<InventoryItem>( in );
cout <<"Load complete." << endl << endl;
in.close();
while (1)
{
string line = "";
cout << "There are currently " << structList.size() << " items in memory.";
cout << endl << 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: Delete a record " << endl;
cout << "5: Save current information " << endl;
cout << "6: Exit the program " << endl;
cout << endl;
cout << "Enter a command 1-6: ";
getline(cin , line);
int rValue = atoi(line.c_str());
system("cls");
ofstream out("data.dat");
switch (rValue)
{
case 1:
addItem(structList);
break;
case 2:
displayRecord(structList);
break;
case 3:
break;
case 4:
break;
case 5:
if (!structList.size()) { cout << "There are no items to save! Enter one first!" << endl << endl; system("pause"); system("cls"); break; }
writeVector(out , structList);
break;
case 6:
return 0;
default:
cout << "Command invalid. You can only enter a command number 1 - 6. Try again. " << endl;
}
out.close();
}
system("pause");
return 0;
}
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;
}
cout << "Save completed!" << endl << endl;
}
ostream &operator<<(ostream &out, const InventoryItem &i)
{
out << i.Description;
out << i.Quantity;
out << i.wholesaleCost << i.retailCost;
out << i.dateAdded;
return out;
}
istream &operator>>(istream &in, InventoryItem &i)
{
in >> 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;
if (in.fail())
{
in >> size;
} else {
size = 0;
}
vector<T> vec;
vec.reserve(size);
for(unsigned int i = 0; i < size; i++)
{
T tmp;
in >> tmp;
vec.push_back(tmp);
}
return vec;
}
template<typename T>
vector<T> addItem(vector<T> &vec)
{
system("cls");
string word;
unsigned int number;
InventoryItem newItem;
cout << "-Add a new item-" << endl << endl;
cout << "Enter the description for the item: ";
getline (cin , word);
newItem.Description = word;
cout << endl;
cout << "Enter the quantity on hand for the item: ";
getline (cin , word);
number = atoi(word.c_str());
newItem.Quantity = number;
cout << endl;
cout << "Enter the Retail Cost for the item: ";
getline (cin , word);
number = atoi(word.c_str());
newItem.retailCost = number;
cout << endl;
cout << "Enter the Wholesale Cost for the item: ";
getline (cin , word);
number = atoi(word.c_str());
newItem.wholesaleCost = number;
cout << endl;
cout << "Enter current date: ";
getline (cin , word);
newItem.dateAdded = word;
vec.push_back(newItem);
return vec;
}
template<typename T>
void printItemDescriptions(vector<T> &vec)
{
int size = vec.size();
if (size)
{
cout << "---------------------------------" << endl;
cout << "| ~ Item Descriptions ~ |" << endl;
cout << "---------------------------------" << endl;
cout << "*********************************" << endl;
for (int i = 0 ; i < size ; i++)
{
cout << "(" << i+1 << ")" << ": " << vec[i].Description << endl;
}
cout << "*********************************" << endl << endl;
}
}
template<typename T>
int displayRecord(vector<T> &vec)
{
string word = "";
string quit = "quit";
int choice = 1;
int size = vec.size();
if (size)
{
printItemDescriptions(vec);
cout << endl;
while (1)
{
cout << "Type \"exit\" to return to the Main Menu." << endl << endl;
cout << "Enter \"list\" to re-display the items." << endl << endl;
cout << endl;
cout << "Pick the number of the item you would like to display: ";
getline (cin , word);
if (convertToLower(word) == "exit") { system("cls"); return 0; }
if (convertToLower(word) == "list") { system("cls"); displayRecord(vec); }
choice = atoi(word.c_str());
if (isValidChoice(size, choice))
{
system("cls");
cout << endl << "[Item (" << choice << ") details] " << endl << endl;
cout << "******************" << endl;
cout << "* Description * " << vec[choice-1].Description << endl;
cout << "******************" << endl << endl;
cout << "******************" << endl;
cout << "*Quantity On Hand* " << vec[choice-1].Quantity << endl;
cout << "******************" << endl << endl;
cout << "******************" << endl;
cout << "* Wholesale Cost * " << vec[choice-1].wholesaleCost << endl;
cout << "****************** " << endl << endl;
cout << "******************" << endl;
cout << "* Retail Cost * " << vec[choice-1].retailCost << endl;
cout << "****************** " << endl << endl;
cout << "******************" << endl;
cout << "* Data Added * " << vec[choice-1].dateAdded << endl;
cout << "****************** " << endl << endl;
} else { system("cls"); cout << "That item doesn't exist!" << endl; cout << "Pick another item or enter \"list\" to see available items." << endl << endl; }
}
} else { cout << "There are currently no items to display." << endl << endl; system("pause"); system("cls"); return 0; }
return 1;
}
bool isValidChoice(int size, int choice)
{
for (int i = 0 ; i <= size ; i++)
{
if (choice == size) { return true; }
}
return false;
}
string convertToLower(string word)
{
for (unsigned int i = 0 ; i < word.size() ; i++)
{
word[i] = tolower(word[i]);
}
return word;
}
解决方案
In your read function:
if (in.fail())
should be:
if ( ! in.fail())
Or better:
int n;
if ( ! (in >> n) ) {
n = 0;
}
这篇关于我做错了我把序列化一个向量与结构到它的.dat文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文