Archived

This topic is now archived and is closed to further replies.

Major

Substituting text...

Recommended Posts

Major    122
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]

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
Major    122
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.

Share this post


Link to post
Share on other sites
Michalson    1657
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.

Share this post


Link to post
Share on other sites
Major    122
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]

Share this post


Link to post
Share on other sites