Changing values and storing new values in file

Pages: 12
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
It doesn't have any way to store a data in file without rewrite the entire file?


Yes you can - but you have to be very careful. You can overwrite data in a file. So if you want to change a number and a number is always written in say a fixed width of 10, then providing the new number has a width less than this then you can overwrite the existing data using tellg() and seekp() as mentioned by @Andy Handy. But this method requires that the original file has the number written like this. If you have control over the format of the file from original creation, then this method is a possible. If the number isn't written as a fixed width and you can't change this - then no.

eg.
If the file has:

101,      2.00,Widget typeAA


and you want to change the 2.00 to say 211.00 then you can as there is space - so the new file becomes

101,   211.00,Widget typeAA


without re-writing the whole file.
If overwriting is now the latest flavor in our imaginings of OP's incoherent and unintelligible intent then just use a binary file with tell/seek.

It's interesting to note that @OP has never cooperated, never learned anything and yet my previously impossible format is now becoming the sample. Wow for me stating the obvious days ago.

I get the impression Cheggs or some other trash source is behind this. It wouldn't be the first time here and all the bullshit system, colors and sleeps provide the distinctive earmarks.
BTW As we all know a container ... <map> ... is the way to go and if OP does in fact want to record all past changes then each Item (Thing FFS) can have a vector data member to keep track of old prices.
Topic archived. No new replies allowed.
Pages: 12