Search and Replace Program

Started by
0 comments, last by kag1 19 years, 10 months ago
Hi, I recently made a Search and Replace program, that is suppose to do multiple searches and replaces on lots of files ..like 1000 different files... how i made it work, is it loads the whole file (.htm) into a std::string, then it does the S&R on the string...deletes the old file, and re writes the string to a new file, making the search and replace complete.. the problem with this is that the files I would be doing this on are rather importantant, and this way just doesn''t seem safe enough to use really, especially when i have to delete the whole file and re-write the whole file just to replace maybe one word..and some of the files, are near 50K in size.. Can somone tell me a better way to do a massive search and replace? or just a search and replace in a file, without having to delete it, then re-write it? thanks alot kag1
Advertisement
There shouldn''t be any problem loading 50k into memory. I''d probably use something other than std::string though, ''plain'' memory should be better. Unless some of the functions std::string provides are essential for your s&r algo.

Since you say that the file is important and you don''t want to mess it up, why not make a backup instead of deleting it?

- read file content
- move file to ''file.bak''
- do s&r
- write file content (to new file)

For complex s&r I''d advise you to take a look at ''regular expressions'' (I think ''boost'' provides a regular expression class that you could use). They can be a powerfull tool if you know how to use them.
How do I set my laser printer on stun?

This topic is closed to new replies.

Advertisement