c++ - 在构造函数中创建二进制文件
问题描述
我写了一个Store
包含Item
. 我创建了两个函数,一是打印文件的内容,一是对记录进行排序并将它们插入回文件中。使用初始化列表,它可以正确显示文件的内容,但说有“文件错误”。如何更正此问题以及在构造函数中创建二进制文件的最佳方法是什么
#pragma once
#include <iostream>
#include <fstream>
#include <cstring>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
struct Item
{
char name[20];
int code;
float price;
int quantity;
Item() { code = 0, price = 0, quantity = 0;
char name1[20] = { 0 }; strcpy_s(name, 20, name1);
}
Item(const char* name, int code, float price, int quantity)
{
strcpy_s(this->name, strlen(name) + 1, name);
this->code = code;
this->price = price;
this->quantity = quantity;
}
friend ostream& operator<<(ostream& os, Item& item)
{
os << "Name: " << item.name << endl;
os << "code: " << item.code << endl;
os << "price: " << item.price << endl;
os << "quantity: " << item.quantity << endl << endl;
return os;
}
bool operator!()
{
return code != 0;
}
};
class Store
{
private:
int size = 100;
fstream file;
string fileName;
public:
Store(string fname);
~Store();
Store& operator+=(Item& myItem);
friend ostream& operator<<(ostream&, Store&);
void sortStore();
};
#include "Store.h"
Store::Store(string fname) : file(fname,ios::binary)
{
Item tmp;
if (!file)
cout << "Error with file" << endl;
for (int i = 0; i < size; i++)
file.write((char*)&tmp, sizeof(Item));
//file.close();
file.open(fname, ios::binary | ios::in | ios::out);
}
Store::~Store()
{
file.close();
}
Store& Store::operator+=(Item& myItem)
{
file.write((char*)&myItem, sizeof(myItem));
return (*this);
}
ostream& operator<<(ostream& out, Store& store)
{
Item tmp;
store.file.seekg(0, ios::beg);
for (int i = 0; i < store.size; i++)
{
store.file.read((char*)&tmp, sizeof(Item));
if (!tmp)
cout << i << ": " << tmp;
}
return out;
}
void Store::sortStore()
{
Item tmp;
vector<Item> items;
file.seekg(0, ios::beg);
while (!file.eof())
{
file.read((char*)&tmp, sizeof(Item));
items.push_back(tmp);
}
sort(items.begin(), items.end(), [](Item& first, Item& second)
{
return first.code < second.code;
});
file.clear();
remove("store.dat");
Store("store.dat");
file.seekp(0, ios::beg);
for (auto it = items.begin(); it < items.end(); it++)
file.write((char*)&(*it), sizeof(Item));
}
int main()
{
Store ourStore("store.dat");
Item apple("apple", 45, 12.2, 10);
Item ananas("ananas", 12, 27, 10);
Item strawberry("strawberry", 1, 24.2, 5);
Item banana("banana",23,14.2, 12);
ourStore.operator+=(apple);
ourStore.operator+=(ananas);
ourStore.operator+=(strawberry);
ourStore.operator+=(banana);
int choice = -1;
while (choice != 0)
{
cout << "1.Sorting, 2 Print" << endl;
switch (choice)
{
case 1:
cout << "Sorting" << endl;
ourStore.sortStore();
case 2:
cout << "Print" << endl;
cout << ourStore << endl;
}
cin >> choice;
}
return 0;
}
解决方案
file
中的局部变量Store::Store
隐藏了成员变量Store::file
。
你想要一个初始化列表:
Store::Store : file(fname, ios::binary)
{
// ...
推荐阅读
- java - ASM ClassReader 无法解析类文件 [class java.lang.Object],可能是由于尚不支持新的 Java 类文件版本
- javascript - json 文件到图表
- mysql - 如何在 heroku 上部署我的 mysql 数据库而不是使用它的 clearDB?
- python - 图像处理的二进制阈值
- python - 在二维 numpy 数组中随机选择一个零
- python - Python - 电子邮件中的多个嵌入图像 - 第一个是嵌入的,其余的是附加的,为什么?
- python - Python ffmpeg 子进程永远不会在 Linux 上退出,适用于 Windows
- amazon-web-services - 我们可以为域和子域使用不同的 NS
- google-cloud-platform - 如何使用 Apache Beam 最新转换从 pcoll 获取最新时间戳?
- javascript - 反应Js | Array of Arrays 解构问题