c++ - 为什么当我从文件加载它的序列化时,我的充满结构的 vector 的大小如此之大?

标签 c++ serialization file vector struct

由于你们中的大多数人可能已经知道我的问题,我正在尝试创建一个程序,可以将多个结构序列化为一个 .dat 文件,通过加载它的序列化将它们读回,编辑内容,然后然后将它们重新写入文件等。这是一个我正在尝试做的库存计划,但我无法让它在我的生活中发挥作用。

我正在加载的文件是空白的。我的程序甚至需要 10 秒才能加载,现在我知道为什么了。这是因为我的 vector 大小大约是 25 万。哦等等......这次我运行它时我的 vector 大小是 5,172,285。那是一个充满结构的相当大的 vector 。没有任何运行时或编译错误,但我很确定我做错了什么。我正在加载的文件也是完全空白的。

代码:

// 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;
}

有人可以简单地告诉我如何将其转换为一个程序,该程序实际上可以将充满结构的序列化 vector 写入文件,然后将它们读回,以便我可以编辑它们并将它们存储回去以供以后加载吗?哦,天哪,这真是一段旅程!

感谢您提供的任何帮助!

最佳答案

你说文件实际上是空的。 readVector 中的第一行是这样的:

in >> size;

您认为实际尺寸最终会是多少?由于它是空的,这将导致您没有检测到的错误。变量 size 将保持未初始化状态 - 因此您会在其中看到奇怪的值,因为它采用了当时内存中恰好存在的任何值。您可以使用检查来检查流的状态:

if (in)

在 bool 上下文中处理它会告诉您是否发生错误 - 这也将涵盖诸如无法读取有效整数变量之类的事情。我建议您应该弄清楚如何在调试器中运行您的程序,您将能够单步执行代码并查看给定时间变量的值。

关于c++ - 为什么当我从文件加载它的序列化时,我的充满结构的 vector 的大小如此之大?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/708684/

相关文章:

android - 在 ffmpeg 中读取 JPEG

c++ - vector 绘图应用程序如何做到这一点?

json - 问题尝试将实体编码为 json

java - 应用程序之间的通信?

Java Kafka 对象序列化器和反序列化器

c# - 如何将 JSON 值保留为字符串?

node.js - Express.js - 如何将 base64 字符串下载为 PDF 文件?

Java 二进制文件下载提前停止

xcode - 如何将我的 .m 文件更改为 .mm 文件?

c++ - GoogleTest SetArgReferee 与 vector <unique_ptr>