0

In the below class error is in the init function where i load the class object I stored in the file to the vector Items.

class Item
{
std::string item_code;
std::string item_name;
std::string unit_name;
unsigned int price_per_unit;
double discount_rate;
static std::vector<Item> Items;
friend std::ostream& operator<< (std::ostream&, Item&);

public:
static void PrintAll();
static void Init();
~Item();
};

Default constructor is the one which reads data from user and writes into file. Below is the code of default constructor.

Item::Item(int a)
{
std::cout << "Item name : ";
std::getline(std::cin, item_name);
std::cout << "Unit (Kg/g/Qty) : ";
std::getline(std::cin, unit_name);
std::cout << "Price per unit : ";
std::cin >> price_per_unit;
std::cout << "Discount Rate : ";
std::cin >> discount_rate;
std::cin.ignore();
std::cout << "Product code (has to be unique) : ";
std::getline(std::cin, item_code);

std::ofstream outfile;
outfile.open("Files\\Items.txt", std::ios::out | std::ios::app);
outfile.write((char*)&(*this), sizeof(Item));
outfile.close();
}

Below is the Init() function for which read access violation is thrown at.

void Item::Init()
{
std::ifstream infile("Files\\Items.txt", std::ios::in);
if (!infile.is_open())
{
    std::cout << "Cannot Open File \n";
    infile.close();
    return;
}
else
{
    Item temp;
    while (!infile.eof())
    {
        infile.read((char*)&temp, sizeof(temp));
        Item::Items.push_back(temp);
    }
}
infile.close();
}

Even though i am checking for eof, read access violation is thrown. Please give me some advice on this issue.

6
  • What does Items.txt look like? You're trying to read directly into an Item, and my gut says that's not what you actually want. Commented Mar 22, 2018 at 6:14
  • Item::Items.push_back(temp); ? Commented Mar 22, 2018 at 6:15
  • you won't be able to read that way. std::string is an object which itself has pointers. You cannot just write it and read it as a block. Commented Mar 22, 2018 at 6:17
  • i have added the part where i write into file. Commented Mar 22, 2018 at 6:22
  • @ThomasMartin That writes to the file the internal representation that this instance of the program is using to represent the data in this particular memory space. It is meaningless as soon as any of that changes. You should NOT be doing this. If you want to write strings to a file, write code to write strings to a file -- don't write random chunks of memory to a file. Commented Mar 22, 2018 at 6:24

2 Answers 2

2
    infile.read((char*)&temp, sizeof(temp));

This fills the temp object with junk from the file. It's supposed to contain valid std::string objects and whatever is in the file, it can't possibly be a valid std::string object. If you don't see why, consider that creating a valid std::string object requires allocating memory to hold the string data -- that's what the std::string constructor does. Reading data from a file can't possibly do this.

A file is a stream of bytes. To write data to a file, you need to define some way to represent that data as a stream of bytes. You need to encode its length if it is variable length. To read it back in, you need to handle the variable length case as well. You need to convert the file data to an appropriate internal representation, such as std::string. This is called "serialization".

Sign up to request clarification or add additional context in comments.

4 Comments

i have added the part where i write object into the file. can you please take a look at it>
@ThomasMartin It writes meaningless junk to the file. Where's the code to write the actual string data? How is the reader supposed to know how many bytes to read? What if sizeof(temp) is less than the length of the string data? This is all wrong in every way.
i understand. i will change the code and write as string
You will need to handle the fact that the string's length can vary somehow. The easiest way is to set a maximum size and always read/write that many bytes for each string.
1

std::string size is variable, you can try the following definition

char item_code[20];
char item_name[20];
char unit_name[20];

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.