[c++] How to edit a specific part of line from a txt file? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

[c++] How to edit a specific part of line from a txt file?

I'm looking for a code which edits only desired part of the line from a txt file. suppose data in file is like: book1 wings of fire 50 book2 xyz xtz xyz 100 book3 abc abc abc 55 how can i change only the numbers part? to be more precise, when a user takes a book, minus 1 from the number. ps: please provide code. I've read explanation and didn't understand any of it. please and thanks for your time. :)

12th Jun 2017, 3:22 PM
anonymous
4 Answers
+ 4
There are 3 possible ways to do it: 0) As Zeke Explained, add a delimiter to the name of the book to stop getline at that point and read the number... But then you will have to change the file by writing the new digit, and then for that you can use seekp seekg tellp and tellg for achieving this... Get the positions of the numbers during reading, and use these positions during writing... 1) Assume a number to be the max size for a book name. Then read after max characters from the fike to get the number. In this case, you will also have to append the strings with empty spaces so that all the strings have lengths = max, and reading is easily possible... You will have to use a delimiter even here, but now you wont have to use seekg tellg etc to get the position of the numbers... 2) Use 3 functions read write and overwrite to maintain and change data in local variables... Similar to something like this : https://www.sololearn.com/discuss/457424/?ref=app
13th Jun 2017, 4:39 AM
Kinshuk Vasisht
Kinshuk Vasisht - avatar
+ 3
Well, I think it would be necessary to have a way to know exactly which part of the file you want to change, like the books for example. The way I'm picturing this in my head is to have an enumeration class: enum class FileLines { Book1 = 0, Book2, Book3, MaxLines }; This way, when you access the file using a loop to look at each line of the text file, you can specify which line you want. For example say the user inputs which book they want using a number menu, 1. Book1 2. Book2 etc... Then the following could happen: if (input == 2) FileLines lineChoice = FileLines::Book2; Now you can start your output or input file stream operations, only accessing the line that you want. I'm sure you've run into the problem where it overwrites everything in the text file every time you try and change only one thing, but I'm pretty sure there are some functions where you can specify that you don't want to overwrite the whole file. I hope this helps! Happy coding :)
12th Jun 2017, 3:57 PM
Zeke Williams
Zeke Williams - avatar
+ 2
Perhaps you could add a delimiter to your text file for formatting. So put a colon or other symbol right before the number: book1 name of book: 100 This way you can get only the number part and do a simple string-to-int process and then back to a string once you've changed it. As far as changing text files in the middle, you will need to save every line then rewrite them all to the file with the changes you've made, which will be your book count. You've picked quite a difficult thing to do here! It's possible and I wish I could help more, but I haven't really used filestreams enough to direct you in a more specific way.
12th Jun 2017, 5:39 PM
Zeke Williams
Zeke Williams - avatar
+ 1
@zeke thanks for your answer, mate. :) just one doubt, how can I change only specific part of the line? example: line is like: book1 name of the book 100 here, 100 is the no. of copies available. how can i change only that "100"?
12th Jun 2017, 5:27 PM
anonymous