lundi 26 septembre 2016

Error in file handling in C++

void modify_product(Product_Details& pr)
    {
        int no;
        int flag = 0;
        cout << "Please Enter The Product Number of The Product\n";
        cin >> no;
        fp.open("Shop.dat", ios::in | ios::out);
        while (fp.read(reinterpret_cast<char*>(&pr), sizeof(Product_Details)) && flag == 0)
        {
            if (pr.product_number == no)
            {
                cout << "Product number: " << product_number << endl;
                cout << "Product name: " << product_name << endl;
                cout << "Price of the Product: " << price << endl;
                cout << "Quantity of the product: " << quantity << endl;
                cout << "Tax on the product: " << tax << "%" << endl;
                cout << "Discount on the product: " << discount << "%" << endl << endl << endl;

                cout << "Please Enter The New Details of Product" << endl;
                /*cout << "Enter the Product number\n";
                cin >> product_number;*/
                cout << "Enter the product_name\n";
                scanf("\n");
                getline(cin, product_name);
                cout << "Enter the price of the product\n";
                cin >> price;
                cout << "Enter the quantity of the product\n";
                cin >> quantity;
                cout << "Enter the tax applied on the product\n";
                cin >> tax;
                cout << "Enter the discount if any on the product\n";
                cin >> discount;

                int pos1 = sizeof(pr);
                int pos = -1 * pos1;
                fp.seekp(pos, ios::cur);
                fp.write(reinterpret_cast<char*>(&pr), sizeof(Product_Details));
                cout << "Record Updated\n";
                flag = 1;
            }
        }
        //  fp.clear();
        //fp.seekp(EOF, ios::cur);

        fp.close();
        if (flag == 0)
            cout << "\nRecord Not Found\n";
    }

Modify function is not overwriting the values in the file. The program is crashing abruptly. I think there is a problem with the file pointer(stream).But I could not find it. I am using visual studio IDE for compilation.

Aucun commentaire:

Enregistrer un commentaire