Substituting text...

Started by
3 comments, last by Major 21 years, 11 months ago
In my prog I open my file,and I look if for example a string exists...if it don't exits I write it at the end of the filoe, ansd if it exists I want to subsitute this string by another. So I just want to erase this line and write after...and I don't want to touch to other lines of the prog... The problem is if the first string is shorter than the second string, all the old string is not erased.It stays some part of the ancient string. For example, in my file: Hello Good Morning Bonjour Salut I want to substitue the third line ("Bonjour") by "Bouh". But with my method I obtain "Bouhour" in the third line..(Bouh + the three last letters of "Bonjour" )... So is there a way to erase a whole line , and just a line not the whole file?? Thanks in advance, and I hope I was understanble with my poor english... [edited by - Major on May 1, 2002 4:32:52 PM]
Advertisement
It looks like when you find a word, you simply write your replacement word over it, without deleting the original string/line. What you need to do is delete the entire word and insert the new word where the old word was.

-Mike

p.s. What language are you using?
quote:Original post by doctorsixstring
It looks like when you find a word, you simply write your replacement word over it, without deleting the original string/line. What you need to do is delete the entire word and insert the new word where the old word was.

-Mike

p.s. What language are you using?


Yes it''s exactly what I want to do...indeed in my example and with my method I just write a word over another...I know that I must do what you say but how...
And of course I forget to mention that I use C++ , and the fstream class to open,read and write into my files.
Thanks for this first answer.
From what you describe I''ll assume your using random access (you have some sort of seek command to let you move around the file freely).

Unfortuately you can''t ''delete'' those extra characters from the file or insert space for more characters. Either you''ll have to load the entire file into memory, edit it (which will include shift large sections of it back and forth to make these adjustments) then save it OR you can create a temporary file and write to that. When you''re done just delete the original file and rename the temporary file.
Yes it's a possibility,I thought about this,but my file always changes during the execution of my application, and so saving,loading etc. can represent a cost for the speed of the application...fortunately my file is not big,but how do you do this if it's a big file...because in my file many change occurs during the execution of the program.


[edited by - Major on May 1, 2002 5:23:54 PM]

This topic is closed to new replies.

Advertisement