Inventory program

Hello, new to C++ and this forum too. I am currently going through object oriented C++ classes at my local college.

I want to write a program that stores inventory items in a text file.
I was curious if there is a way to read/write to a file but also remove specific data from it (without deleting the whole thing) So if I ran out of nails for example, I can remove it from my inventory.

I have read online and some archived chats on this forum that using a vector and then deleting from a vector would be ideal, however, I have not used vectors to much.

Should I go back and read up more before attempting, or is there a simpler way I am missing?

Thank you!
You'd have to read the entire contents of your file into memory, manipulate the data to delete whatever items should be tossed, and then write the file back to the text file from the beginning. You can't manipulate the file contents directly on the HD.

The container used to hold the file's contents could be a std::vector. It really depends on how the data is structured. Comma-separated values, space-separated values are two common ways to store data.

https://www.learncpp.com/cpp-tutorial/an-introduction-to-stdvector/
A struct might come in handy as well, to "tie together" the data bits used to represent the particulars of each inventory item.

https://www.learncpp.com/cpp-tutorial/introduction-to-structs-members-and-member-selection/
that using a vector and then deleting from a vector would be ideal, however, I have not used vectors to much.

Should I go back and read up more before attempting, or is there a simpler way I am missing?


Yes.

Yes and no.

Using a struct to represent the data for 1 record in the file and having a vector of that struct type to hold the contents of the file is usually the way to go. Then the vector contents is manipulated as required and at the end is written back to the file.
Topic archived. No new replies allowed.