Access violation writing location error when i try to read from file

0

this is where i write to a file ->

void Supermarket_Billing_System::Add_and_WriteNewProduct(Product P)
{
    ofstream fl;
    fl.open("ProductList", ios::out | ios::binary);
    fl.write((char *) &P, sizeof(P));
    fl.close();
}

this is where i read from a file ->

void Supermarket_Billing_System::Read_DisplayProductFromProductList()
{
    Product P;
    int x;
    ifstream fp;
    fp.open("ProductList", ios::in);
    fp.seekg(0, ios::beg);
    fp.read((char *) &P, sizeof(P));
    x = P.GetProduct_qty();
    fp.close();
}

Product class looks like this ->

class Product
{
private:
     long int Product_no;
     std::string Product_name;
     double Product_price;
     int Product_qty;
     double Product_tax;
     double Product_dis;

public:

    //Constructor
    Product();
    Product(long int, string, double, int, double, double);

    //All Getters methods
    long int GetProduct_no();
    string GetProduct_name();
    double GetProduct_price();
    int GetProduct_qty();
    double GetProduct_tax();
    double GetProduct_dis();

    //All Setters methods
    void SetProduct_no(long int);
    void SetProduct_name(string);
    void SetProduct_price(double);
    void SetProduct_qty(int);
    void SetProduct_tax(double);
    void SetProduct_dis(double);

    void Accept_Product();
    void Accept_ProductForBilling();
    void Display_Product();
};

When i try to read from the file by calling Read_DisplayProductFromProductList() it gives me error ->

Unhandled exception at 0x6803ad54 (msvcp100d.dll) in Supermarket_Billing_System.exe: 0xC0000005: Access violation writing location 0xfeeefeee.

c++
visual-c++
asked on Stack Overflow May 29, 2014 by chirag_lad

4 Answers

1

Your Product class is not trivially copyable due to the std::string member; you are not writing the actual string that Product_name holds in your write method, only the binary representation of a std::string, and the internal string pointer will almost certainly point to an invalid location when you read. Either way attempting to perform a bytewise copy of a non-trivially copyable class is undefined behavior.

You also haven't passed the binary flag when you open your file for reading

fp.open("ProductList", ios::in);

which will cause issues on Windows if there are any \r\n (0x0A 0x0D) sequences in the file you are reading.

You will either need to use a char array for your string, or manually handle the serialization of each member. The simplest way would be to use stream friend functions to write your object to a plain text file

answered on Stack Overflow May 29, 2014 by user657267 • edited May 29, 2014 by user657267
1

I believe you shouldn't read into std::string like if it were a POD (plain old data). std::string Product_name is a member of your Product class and when you read from the stream, it likely spoils the string.

Similarly, you shouldn't write std::string like that (as part of Product).

answered on Stack Overflow May 29, 2014 by Karadur
0

I faced a similar issue, when i did write operation on a file using ofstream in a gSOAP client. However when i switched to FILE (stdio.h) and fwrite() the access violation error was gone!

answered on Stack Overflow May 15, 2018 by Martin • edited May 16, 2018 by Martin
-1

It may because the open file error. You can print the return value to see if it is NULL. When you compile your code.

Pay attention to the warning message. Sometimes, it may just a warning when you compiler, but got a crash when you run it.

answered on Stack Overflow Aug 16, 2019 by Jie Yin

User contributions licensed under CC BY-SA 3.0