Hello adeel zamann,
There are two ways to do this:
First you could create a struct:
1 2 3 4 5 6
|
struct Product
{
std::string s_id;
std::string s_name;
double price{};
};
|
Then in "main" do:
1 2 3 4
|
constexpr unsigned int MAXSIZE{ 50u };
unsigned int count{};
Product products[MAXSIZE];
|
Then read the input file into each element of the array and add to "count" to keep track of how much of the array is used. This way you can work with the computers memory and not have to read the file every time you need, which is a bit slower. When the program is about to finish write the array to the file replacing what is there with the array.
Option 2 would be to read the file and using "tellg()" and "seekp()" to get the file pointer position and move the file pointer.
The part I do not know is if you are required to change the file as you read it or if you can do something different.
Either way you first need to write to the file in a way that is easily to read and the be able to read the file before you even start to work on changing the file.
I am not the expert here when it comes to writing something new to an existing file, but when it comes to the price you may have to write a fixed amount of space to the file, so that you have enough room to change the price without causing a problem with the next entry, which is the part I am not sure about.
Andy