Sign in to follow this  
gandolf1212

Changing a line in a file with C++

Recommended Posts

I have a large file, in this format: XXXXXX XXXXXXXXXX XXXXXX XXXXXX XXXXXX XXXXXXXX I am trying to write a function that will replace XXXXXX XXXXXXXXXX with #XXXXXX XXXXXXXXXX But so far all I can manage to do is write over the entire file. While I could just read in the whole file, then write it all back out, it doesn't seem like a good solution to the problem. I am using fstream, and tried using tellg and seekp to move my pointer, but it just killed my file. Anyone have any advice?

Share this post


Link to post
Share on other sites
You're going to have to write the whole file out again (sort of):

The file is contiguous on your hard-drive. This means that in order to insert something, every single thing afterwards will have to be "pushed back" by the size of that thing (in this case, one byte). Everything beforehand can stay where it is. You will have to iterate from the end of the file to the position where you want to insert, moving everything "down" one byte, until you get to the point where you want to write to, at which point you write your character. There are faster ways (copying the data as a whole over with an offset of one byte), but I don't know how, since my IO-fu is weak.

Share this post


Link to post
Share on other sites
Entirely out of curiosity, why are you using c++?

Awk, Perl, Python and numerous other scripting languages or sed/grep would be able to do the same thing. Admittedly they would be slower but unless you are working with truly enourmous files the development time saved would be well worth it.

Share this post


Link to post
Share on other sites
Quote:
Original post by chowe6685
Entirely out of curiosity, why are you using c++?

Awk, Perl, Python and numerous other scripting languages or sed/grep would be able to do the same thing. Admittedly they would be slower but unless you are working with truly enourmous files the development time saved would be well worth it.


Apart from the fact the program is about %60 done...

Of your list, I have only used Perl, but I imagine your write when you say any of those languages can do it better than C++. Still, when I am done, I want a non-bloated, easily distributable program - and mine is currently 565kb and only uses more than 10% of my processing power when it first starts (binding a socket).

Also, my problem got changed by the forums. My lines are like this:


XXXXXX XXXXXXXXXX
XXXXXX XXXXXX
XXXXXX XXXXXXXX


There are two spaces. I want to add the #, shift the first group of X's, and drop a space.

Share this post


Link to post
Share on other sites
Off hand:

Find the right place in the file where you want to put the #
temp ← '#'
Repeat until you've reached the character you want to overwrite:
chr ← read a character
write temp
temp ← chr

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this