0

I'm trying to create a program where I can write a string to the end of a certain line in a text file.

For example, this is the text file with just a bunch of random numbers:

12 23
53 23

Now what if I had a program that adds another two-digit number to the end of the first line (or any specified line), keeping in mind that the line will keep expanding if I kept running the program over again.

So after running the program, the text file would look something like this:

12 23 34
53 23

What are some ways to solve this?

Jonathan Wood
  • 65,341
  • 71
  • 269
  • 466

2 Answers2

2

If I understand the question, this is not possible. The problem is that if you make one line longer, it would overwrite the next line. You seem to be expecting the following lines to shift down to make room but that's not how it works.

Unless your file is huge, the best approach is to load the file into memory, modify the data in memory, and then create a new file to save the results. (You can save to the same file if needed.)

Other algorithms involve using blocks of a fixed size, having a linked list in your file, but it sounds like you want you file to be normal text and so those likely wouldn't work.

Jonathan Wood
  • 65,341
  • 71
  • 269
  • 466
  • Could you direct me to an example? Thanks! – Firzan Armani Feb 04 '15 at 18:31
  • @FirzanArmani: You've provided very few details. I've raised the type of issues that need to be considered (files size, file format, etc.). You've responded but I still don't have any additional information about your requirements. It would be more productive for you to think about it and then ask more specific questions about where you get stuck. – Jonathan Wood Feb 04 '15 at 18:33
  • @Johnathan Wood Ahh sorry about that, I just started learning C++ recently in school and this is for an idea I had. I presume the .txt file wouldn't be that big as it is just a small program to demonstrate its functionality. I'd like to know about the approach of loading the txt file into memory, modifying and then overwriting the txt file, especially since this is beyond what I've learned so far. What should I search up on to look up on this? – Firzan Armani Feb 04 '15 at 18:56
  • @FirzanArmani: There are a few different class of routines to use. I'm old school and might just use `fopen()` to open the file, `fgets()` to read each line of the file. You could then store or scan this data, modifying any data as needed, and then use `fopen()` to create a new file, an `fputs()` to write each line of the new file. – Jonathan Wood Feb 04 '15 at 20:05
0

You should never try to edit a text file in place. The most common way is to write the edited file in a temporary location, and at the end rename it to the old name (after removing the original file).

The other way is (provided the file is small enough) to load everything in memory and to rewrite the file from the beginning. But never do that on sensitive files because if your program (or your computer or electric power) crashes in the middle, you file will be definitively lost.

Serge Ballesta
  • 143,923
  • 11
  • 122
  • 252