# Writing to the middle of a .txt file in C++



## hitman_in_wi (Aug 2, 2004)

Hey guys, is there a way to replace a line in the middle of a text file without bothering everything else?

E.g.

someFile.txt
-----------
...
Joe
Brian
Robert
Deb
...


Lets say I want to change 'Robert' to 'Bob'. Should I simply rewrite the entire file? Or can I just change that one line?


----------



## Chicon (Jul 29, 2004)

Hi hitman_in_wi,

Is it necessary to reinvent the powder or is it homework ? Just use Notepad to do the job.


----------



## Shadow2531 (Apr 30, 2001)

Things you will want to consider: (using robert as an example)

Is the line number that robert is on, always known? (probably not)

Do you want to replace just the first instance of robert or all instances or just some?

If an instance of robert has spaces before and or after it, do you want to replace that instance too? If so, do you want to preserve the spaces on replacement? (It the entries for each line were typed in, there could be a few spaces after it.)

I suggest you create two functions: a scanfile function and a writefile function. Also use a vector to hold the file content.

The scanfile function would (via a loop), read each line into the vector , except if the line matches the conditions you want, which in that case, the replacement text would be stuffed into the vector instead.

The writefile function would just write the contents of the vector to the file.

You show us some effort and we'll show you some examples. Meaning; post how far you've gotten.


----------



## hitman_in_wi (Aug 2, 2004)

Basically, I have the list read into a linked list at program startup. Whenever I want to change a single line in the text file, I change the entry in the list, and then rewrite the entire text file from the list. 

This kinda sounds like what you are suggesting, so I guess thats how Ill keep it. I was just wondering if rewriting the entire file was unnecessary.

I guess it would take just as long tho to scan the file and replace a single line as it takes to simply write the entire file.


----------



## Shadow2531 (Apr 30, 2001)

Sounds like you are doing it O.K. Rewriting the file is fine. Only look for an alternative if the file is so huge, that rewriting it becomes a problem.

Keep in mind, if you haven't taken the necessary precautions and your program crashes, the file could be butchered.

As for just modifying a file without rewriting it, I'll look into it.


----------



## hitman_in_wi (Aug 2, 2004)

ya, the crashing thing has crossed my mind...what would be necessary precautions?

perhaps something like:

copy the old file to a new file name
upon completion of rewriting the file, delete the copy?

Is that all that I would need?


----------



## Shadow2531 (Apr 30, 2001)

You got the right idea, but first make sure that if your program cannot read the file, that it doesn't try to write. (that you knew though)

I would copy the input file to filename.bak first and then go ahead with the rewrite.

In addition, you could compare that the backup file matches the input file before you overwrite the input file.

That would of course overwrite older backups. If you didn't want that, you could append to the backup file. Then the backup file would be a log of all backups. That could get large depending...

You could also have the program accept an argument, which you specify to define the name of the backup file and just have a new backfile each time. (maybe by date etc.)


----------



## hitman_in_wi (Aug 2, 2004)

awesome, thank you very much for your help shadow


----------

