繁体   English   中英

fstream读写函数错误

[英]fstream read and write functions errors

我正在创建这个将一些项目存储到 (.dat) 文件中的程序。该程序似乎运行顺利,但是当我尝试从文件中读取数据时,我在屏幕上看到了特殊字符,但我没有看到任何数据尝试保存,已正确保存,请注意,当我在main()中运行类似代码时,它可以正常工作,它实际上向我显示了正确的 output.. 请帮助我,我真的不知道该怎么做

void viewFile() {
    Product item;
    ifstream file("products.dat", ios::binary);
    if (!file.read(reinterpret_cast<char*>(&item), sizeof(Product))) {
        cout <<"failed to read";
        system("exit");
    }
    item.viewProduct();
    while (!file.eof()) {
        item.viewProduct();
        cout <<endl <<endl;
        file.read((char *)(&item), sizeof(Product));
    }
    file.close();
}
void addProductToInventory() {
    string name;
    int quantity;
    int pricePerItem;
    ofstream obj("products.dat", ios::out | ios::binary);

    Product item;
    int numberOfProducts;
    cout <<"enter the number of products you want to add in this inventory: ";
    cin >> numberOfProducts;

    for (int counter = 1; counter <= numberOfProducts; counter++) {
        Product product;        
        cout <<"enter the name of the object: ";
        cin >> name;
        cout <<"enter the quantity of the product: ";
        cin >> quantity;
        cout <<"enter the price of this product (per item): ";
        cin >> pricePerItem;

        product.setName(name);
        product.setQuantity(quantity);
        product.setPricePerItem(pricePerItem);

        if (!obj.write(reinterpret_cast<char*>(&product), sizeof(Product))) {
            cout <<"failed writing\n";
            system("exit");
        }
        cout <<"item added\n\n";
    }
    obj.flush();
    obj.close();
}

这是我在main()中的代码,它的工作原理完全相同。我猜

ofstream file ("products.dat", ios::out | ios::binary | ios::trunc);
    Product p1("hammer", 12, 3);
    Product p2("screw driver", 43, 1);
    if (!file.write(reinterpret_cast<char*>(&p1), sizeof(Product))) {
        cout <<"failed to write";
        system("exit");
    }
    file.write(reinterpret_cast<char*>(&p2), sizeof(Product));
    file.close();
    ifstream file2("products.dat", ios::out | ios::binary);
    if (!file2.read(reinterpret_cast<char*>(&p1), sizeof(Product))) {
        cout <<"failed to read";
        system("exit");
    }
    while (!file2.eof()) {
        p1.viewProduct();
        cout <<endl <<endl;
        file2.read((char *)(&p1), sizeof(Product));
    }
    file2.close();
}

附注:如果这是一个混乱的问题,我真的很抱歉。我已经调试了几个小时,现在我什至无法想清楚。

让我们从思考指针的工作原理开始。

char* a = new char[2];
a[0] = 'a';
a[1] = 'b';
char* b = a;
std::cout << a[0] << a[1]; //prints ab
std::cout << b[0] << b[1]; //prints ab
a[1] = 'c';
std::cout << b[0] << b[1]; //prints ac
delete[] a;
std::cout << b[0] << b[1]; //prints random garbage because the memory that b points at is deleted

让我们开始将这些指针写入文件

char* a = new char[2];
a[0] = 'a';
a[1] = 'b';
ofstream outFile("products.dat", ios::binary | ios::trunc);
outFile.write(reinterpret_cast<char*>(&a), sizeof(a));
outFile.close();
{
    ifstream inFile("products.dat", ios::binary);
    char* b;
    inFile.read(reinterpret_cast<char*>(&b), sizeof(b));
    std::cout << b[0] << b[1]; //prints ab
    a[1] = 'c';
    std::cout << b[0] << b[1]; //prints ac
}
delete[] a;
{
    ifstream inFile("products.dat", ios::binary);
    char* b;
    inFile.read(reinterpret_cast<char*>(&b), sizeof(b));
    std::cout << b[0] << b[1];//prints random garbage because the memory that b points at is deleted
}

std::string 包含指向动态分配的 memory 的指针。

你应该做这样的事情。

void viewFile() {
    ifstream file("products.dat", ios::binary);
    while (!file.eof()) {
        int nameLength = 0;
        if (!file.read(reinterpret_cast<char*>(&nameLength), sizeof(nameLength))) {
            cout <<"failed to read";
            system("exit");
        }
        std::string name{ nameLength, '?' };
        if (!file.read(name.data(), sizeof(char) * nameLength) {
            cout <<"failed to read";
            system("exit");
        }
        int quantity;
        if (!file.read(reinterpret_cast<char*>(&quantity), sizeof(quantity)) {
            cout <<"failed to read";
            system("exit");
        }
        int pricePerItem;
        if (!file.read(reinterpret_cast<char*>(&pricePerItem), sizeof(pricePerItem)) {
            cout <<"failed to read";
            system("exit");
        }
        Product item{ std::move(name),quantity, pricePerItem };
        item.viewProduct();
    }
    file.close();
}
void addProductToInventory() {
    string name;
    int quantity;
    int pricePerItem;
    ofstream obj("products.dat", ios::out | ios::binary);

    int numberOfProducts;
    cout <<"enter the number of products you want to add in this inventory: ";
    cin >> numberOfProducts;

    for (int counter = 1; counter <= numberOfProducts; counter++) {   
        cout <<"enter the name of the object: ";
        cin >> name;
        cout <<"enter the quantity of the product: ";
        cin >> quantity;
        cout <<"enter the price of this product (per item): ";
        cin >> pricePerItem;

        int nameLength = name.size();
        if (!obj.write(reinterpret_cast<char*>(&nameLength), sizeof(nameLength))) {
            cout <<"failed writing\n";
            system("exit");
        }
        if (!obj.write(name.data(), sizeof(char) * nameLength) {
            cout <<"failed writing\n";
            system("exit");
        }
        if (!obj.write(reinterpret_cast<char*>(&quantity), sizeof(quantity))) {
            cout <<"failed writing\n";
            system("exit");
        }
        if (!obj.write(reinterpret_cast<char*>(&pricePerItem), sizeof(pricePerItem))) {
            cout <<"failed writing\n";
            system("exit");
        }
        cout <<"item added\n\n";
    }
    obj.flush();
    obj.close();
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM