Re: searching in a text file with a specified string and deleting the string(URGENT)

Tech-Archive recommends: Speed Up your PC by fixing your registry



Sounds like a class assignment. Should we really be doing your homework?

See below...
On Thu, 21 Jun 2007 21:26:32 -0700, rindam2002@xxxxxxxxx wrote:

Hi
I have 2 text file which is having its content as follows...
---------------------text file 1-----------------------------
atlanta
moris
hangaroo
--------------------
-----------upto random number of lines
------------------end of text file 1------------------------

---------------------text file 2-----------------------------
www.abc.com
www.sea.com
www.random.com
--------------------
-----------upto random number of lines
------------------end of text file 2------------------------

Now my requirement is that ->
User will input a string value which may or may not exist in
text file1.If it exists in text file1,then that string will be deleted
from text file1 and the corresponding string from the same line no. in
text file2 will also be deleted.Example:-suppose user has entered
moris,then moris,which is in line no.2 of text file 1 will be
deleted.the string in line no.2 of text file 2 is www.sea.com,so that
will also be deleted.Now line no.3 in both files will come to line no.
2,line no.4 in both files will come to line no.3,line no. 5 in both
files will come to line no. 4 and so on upto the end of file.if the
string entered by user is not in text file1 then a message will come
saying that entered string is not in file.How will I do that.Plz help
me with sample code.I am using FILE structure to open ,read write and
close the files.So can u plz do these all things using FILE structure?
I f yes then it will be a great help for me.Plz help me with sample
code.Thanks in advance.

Sure, you can use File. Not a good choice, but you can use it.

Open file 1
Open file 2
Read a line from file 1
Read a line from file 2
Compare the keyword
If it is not equal, write both lines back out to temporary files file T1 and file T2
If it is equal, don't write the lines out
repeat until EOF

close all four files
delete file 1
rename T1 to file1
delete file 2
rename T2 to file 2

Note the above algorithm will delete ALL matching instances.

By the way, I would not use this technique because it is slow, inefficient, requires two
file deletes and renames, and is not terribly robust. If I had a guarantee that file1 and
file2 were both small files (small means < 100MB), I'd open them both as memory-mapped
file, do the deletions in-memory, reset the file length, and close the files. Much more
robust.
joe

Joseph M. Newcomer [MVP]
email: newcomer@xxxxxxxxxxxx
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm
.



Relevant Pages