C++ fstream help, newbie question. | SoloLearn: Learn to code for FREE!

+1

C++ fstream help, newbie question.

Sorry if this is a stupid question. This is for an inventory management program I'm building for a friend. I was wondering how and if it's possible to modify a specific line # of a text file containing my Items and their amounts. I'm using fstream to read and write text file. I've looked online but I can't seem to figure it out. Would I have to just save the lines values and completely delete and rewrite the data to the end of the file or can I modify certain elements of the line? If so, how?

7/21/2017 4:11:43 PM

John Drennan

9 Answers

New Answer

+10

You will have to read and write all the contents to arrays, modify that specific array containing the data you want to modify, and then write all the data back to the file. Writing to a file automatically overwrites everything the file contains.

+1

Seems overkill but that's what I was afraid of. Thanks Hasty !

+1

The STL provides a sort template with which you should be able to sort every random-access iterator easily. #include <algorithm> #include <vector> #include <iostream> int main() { std::vector<string, 3> s = {"b", "a", "c"}; std::sort(s.begin(), s.end()); for (auto a : s) { std::cout << a << " "; } } Not tested. ;) It should work for simple string arrays/vectors. multidimensional need a little more work.

+1

Better create a struct for each item and insert them into an array. struct Item { string name; string category; unsigned int quantity; } vector<Item> items; // ... // Some magic populating the vector // ... std::sort(items.begin(), items.end(), [](const Item & a, const Item & b) { return a.name < b.name; }); Again not tested.

0

It seems overkill but there is no other way as files are just lines of bytes on your harddrive. Each time you want to add something new you have to move everything after the insertion point. If you don't want to add but change something that's another matter. You could preallocate some space with, for example, spaces. Then you just have to seek the part that you want to overwrite and overwrite it. Or you could just use a database, whatever you like more. ;)

0

I got it working. It's mainly for experience, I've only been using c++ for about a week now and I'm just trying to learn everything. If I can create something somebody can make use of in the process then even better. Haven't delved into databases yet, are they even part of c++(not counting multidimensional arrays)? Nothing like sql databases I'm assuming.

0

Well, i really meant sql databases, as C++ has no build-in database facilities. But you don't necessarily have to use MySQL, as you would have to setup a server for that, but more something like SQLite. You could also use a file for each item, but that's a rather uncommon approach.

0

Im using one file that contains every item, their quantity, and category. It's working quite well so far now if only I could pass an array to a method to arrange the item list in alphabetical order. That one's got me stumped.

0

That may work. they are one dimensional arrays, I set up separate arrays items [], quantity[], and category[]. Just so I don't have to convert the quantity from str to int. one item takes up 3 lines in my txt file and my program reads lines 0-2,3-5,etc and stores them as items[0],quantity[0], category [0], etc. So for any Item [location] the [location] for the other arrays is the same. Therefore only the item array will need alphabetized and. I can change the others accordingly. My code isn't pretty but I'm accepting that fact since it's only my second program(calculator was my first). Which I've greatly cleaned up and improved since uploading it here, I think it's now 21 lines. Sorry if I'm rambling, I'm getting addicted to coding. Unfortunately I'm now dreaming in code. literally.