RError.com

RError.com Logo RError.com Logo

RError.com Navigation

  • 主页

Mobile menu

Close
  • 主页
  • 系统&网络
    • 热门问题
    • 最新问题
    • 标签
  • Ubuntu
    • 热门问题
    • 最新问题
    • 标签
  • 帮助
主页 / 问题 / 1547755
Accepted
enderline13
enderline13
Asked:2023-10-25 03:40:25 +0000 UTC2023-10-25 03:40:25 +0000 UTC 2023-10-25 03:40:25 +0000 UTC

无法从二进制文件中正确读取数字

  • 772
class PizzeriaDB {
public:
    ~PizzeriaDB() = default;
    std::vector<Pizza> getPizzasAvailable() const;
    void addClient(std::string, std::string);
    void addEmployee(const std::string& name);
    void deleteEmployee(const std::string& s);
    void addPizza(const std::string& name, double price);
    void deletePizza(const std::string& s);
    void setAdminKey(const std::string& s);
    bool AdminIsValid(const std::string&) const;
    bool ClientIsValid(const std::string&, const std::string&) const;
    void newOrder(const Order& o);
    void complete_order();
    void GetDB();
    void SaveDB();
private:
    std::string AdminKey = "superadmin";
    std::vector<Pizza> availablePizzas;
    std::vector<Employee> workers;
    std::map<std::string, std::string> clientData;
    std::queue<Order> current_orders;
};

class Employee {
public:
    Employee() = default;
    Employee(const std::string& name, bool free = true);
    void doWork(const Order& o);
    bool isFree() const;
    std::string getName() const;
    void SetName(const std::string& n) {
        name = n;
    }

    void SetFree(bool f) {
        free = f;
    }

    // Методы для получения данных
    const std::string& GetName() const {
        return name;
    }

    bool IsFree() const {
        return free;
    }
private:
    std::string name;
    bool free = 1;
};

class Pizza {
public:
    Pizza() = default;
    ~Pizza() {};
    Pizza(const std::string& type, double price, int amount = 1);
    std::string getType() const;
    double getPrice() const;
    int getAmount() const;
    std::string getName() const;
    void SetPizzaType(const std::string& type) {
        pizza_type = type;
    }
    void SetPrice(double p) {
        price = p;
    }
    void SetAmount(unsigned int a) {
        amount = a;
    }
    const std::string& GetPizzaType() const {
        return pizza_type;
    }
    double GetPrice() const {
        return price;
    }
    unsigned int GetAmount() const {
        return amount;
    }
private:
    std::string pizza_type;
    double price = 1;
    unsigned int amount = 1;
};

void PizzeriaDB::SaveDB() {
    // Запись PizzaCatalogue
    out.open("PizzaCatalogue.bin", std::ios::binary | std::ios::out);
    if (!out) {
        std::cout << "Failed to open PizzaCatalogue.bin for writing." << std::endl;
        return;
    }
    size_t pizzaCount = availablePizzas.size();
    out.write((char*)&pizzaCount, sizeof(size_t));
    for (const auto& pizza : availablePizzas) {
        size_t typeLen = pizza.GetPizzaType().size();
        out.write((char*)&typeLen, sizeof(size_t));
        const std::string& pizzaType = pizza.GetPizzaType();
        out.write(pizzaType.c_str(), typeLen);
        double price = pizza.GetPrice();
        out.write((char*)&price, sizeof(double));
    }
    out.close();

    // Запись WorkersDB
    out.open("WorkersDB.bin", std::ios::binary | std::ios::out);
    if (!out) {
        std::cout << "Failed to open WorkersDB.bin for writing." << std::endl;
        return;
    }
    size_t workerCount = workers.size();
    out.write((char*)&workerCount, sizeof(size_t));
    for (const auto& worker : workers) {
        size_t nameLen = worker.GetName().size();
        out.write((char*)&nameLen, sizeof(size_t));
        out.write(worker.GetName().c_str(), nameLen);
    }
    out.close();
}

void PizzeriaDB::GetDB() {
    // Чтение PizzaCatalogue
    in.open("PizzaCatalogue.bin", std::ios::binary);
    if (!in) {
        std::cout << "Failed to open PizzaCatalogue.bin for reading." << std::endl;
        return;
    }
    if (in.peek() == std::ifstream::traits_type::eof()) {
        // Файл пуст, пропускаем чтение
        return;
    }
    size_t pizzaCount;
    in.read((char*)&pizzaCount, sizeof(size_t));
    availablePizzas.resize(pizzaCount);
    for (size_t i = 0; i < pizzaCount; ++i) {
        Pizza pizza;
        size_t typeLen;
        in.read((char*)&typeLen, sizeof(size_t));
        std::string pizzaType(typeLen, '\0');
        in.read(&pizzaType[0], typeLen);
        pizza.SetPizzaType(pizzaType);
        double price;
        in.read((char*)&price, sizeof(double));
        pizza.SetPrice(price);
        availablePizzas[i] = pizza;
    }
    in.close();

    // Чтение WorkersDB
    in.open("WorkersDB.bin", std::ios::binary);
    if (!in) {
        std::cout << "Failed to open WorkersDB.bin for reading." << std::endl;
        return;
    }
    if (in.peek() == std::ifstream::traits_type::eof()) {
        // Файл пуст, пропускаем чтение
        return;
    }
    size_t workerCount;
    in.read((char*)&workerCount, sizeof(size_t));
    workers.resize(workerCount);
    for (size_t i = 0; i < workerCount; ++i) {
        Employee employee;
        size_t nameLen = 20;
        in.read((char*)&nameLen, sizeof(size_t));   
        std::string name(nameLen, '\0');
        in.read(&name[0], nameLen);
        employee.SetName(name);
        bool isFree;
        in.read((char*)&isFree, sizeof(bool));
        employee.SetFree(isFree);
        workers[i] = employee;
    }
    in.close();
}

对于披萨,一切都被记录下来并很好地阅读。但对于员工来说,就是第二个不读名字大小的员工。输入一个随机的大数字(可能是size_t限制),然后由于内存不足而无法构造字符串。本质上,披萨和工人的写入和读取算法是相同的。

c++
  • 1 1 个回答
  • 23 Views

1 个回答

  • Voted
  1. Best Answer
    Harry
    2023-10-25T03:45:31Z2023-10-25T03:45:31Z

    所以,我们正在为员工写信......

    for (const auto& worker : workers) {
        size_t nameLen = worker.GetName().size();
        out.write((char*)&nameLen, sizeof(size_t));
        out.write(worker.GetName().c_str(), nameLen);
    }
    

    字符串的长度,字符串。全部。

    我们在读什么?

    in.read((char*)&nameLen, sizeof(size_t));   
    std::string name(nameLen, '\0');
    in.read(&name[0], nameLen);
    employee.SetName(name);
    bool isFree;
    in.read((char*)&isFree, sizeof(bool));
    

    字符串、字符串、布尔变量的长度。

    不需要进一步解释吗?粗略地说,如果你从钱包里拿走了一些你没有放在那里的东西,麻烦不会让你等待:)

    • 3

相关问题

  • 编译器和模板处理

  • 指针。找到最小数量

  • C++,关于枚举类对象初始化的问题

  • 函数中的二维数组

  • 无法使用默认构造函数创建类对象

  • C++ 和循环依赖

Sidebar

Stats

  • 问题 10021
  • Answers 30001
  • 最佳答案 8000
  • 用户 6900
  • 常问
  • 回答
  • Marko Smith

    我看不懂措辞

    • 1 个回答
  • Marko Smith

    请求的模块“del”不提供名为“default”的导出

    • 3 个回答
  • Marko Smith

    "!+tab" 在 HTML 的 vs 代码中不起作用

    • 5 个回答
  • Marko Smith

    我正在尝试解决“猜词”的问题。Python

    • 2 个回答
  • Marko Smith

    可以使用哪些命令将当前指针移动到指定的提交而不更改工作目录中的文件?

    • 1 个回答
  • Marko Smith

    Python解析野莓

    • 1 个回答
  • Marko Smith

    问题:“警告:检查最新版本的 pip 时出错。”

    • 2 个回答
  • Marko Smith

    帮助编写一个用值填充变量的循环。解决这个问题

    • 2 个回答
  • Marko Smith

    尽管依赖数组为空,但在渲染上调用了 2 次 useEffect

    • 2 个回答
  • Marko Smith

    数据不通过 Telegram.WebApp.sendData 发送

    • 1 个回答
  • Martin Hope
    Alexandr_TT 2020年新年大赛! 2020-12-20 18:20:21 +0000 UTC
  • Martin Hope
    Alexandr_TT 圣诞树动画 2020-12-23 00:38:08 +0000 UTC
  • Martin Hope
    Air 究竟是什么标识了网站访问者? 2020-11-03 15:49:20 +0000 UTC
  • Martin Hope
    Qwertiy 号码显示 9223372036854775807 2020-07-11 18:16:49 +0000 UTC
  • Martin Hope
    user216109 如何为黑客设下陷阱,或充分击退攻击? 2020-05-10 02:22:52 +0000 UTC
  • Martin Hope
    Qwertiy 并变成3个无穷大 2020-11-06 07:15:57 +0000 UTC
  • Martin Hope
    koks_rs 什么是样板代码? 2020-10-27 15:43:19 +0000 UTC
  • Martin Hope
    Sirop4ik 向 git 提交发布的正确方法是什么? 2020-10-05 00:02:00 +0000 UTC
  • Martin Hope
    faoxis 为什么在这么多示例中函数都称为 foo? 2020-08-15 04:42:49 +0000 UTC
  • Martin Hope
    Pavel Mayorov 如何从事件或回调函数中返回值?或者至少等他们完成。 2020-08-11 16:49:28 +0000 UTC

热门标签

javascript python java php c# c++ html android jquery mysql

Explore

  • 主页
  • 问题
    • 热门问题
    • 最新问题
  • 标签
  • 帮助

Footer

RError.com

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

帮助

© 2023 RError.com All Rights Reserve   沪ICP备12040472号-5