Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Replace string in file

Status
Not open for further replies.

ej2k

Programmer
Jan 3, 2001
4
GB
I have two questions:
Firstly, is it possible to replace a string in a file.

Secondly, (and linked to the first question), is it possible to manipulate some sort of cursor, in order to find this string within the file. I currently have the words in the file held in an array of pointers.

Thanks in advance,
EJ
 
Of course it is possible... The standard way to do it is to open 2 files, from one you read... and write in the other one... applying certain conditions on the output (ie. replacing your string).

As for your 2nd question... what do you mean by "manipulate some sort of cursor"? there is the file pointer as cursor which is used by the different ops, in the same general way - it shows how far you're in a file (from the beginning).

To locate more easily your string, you should use some index file along with the file where the string is located.

I gave you just some general ideas, hope this helps.
 
You could also think of over writing the current file too, or to make things simpler (but lenghtier) read all the contents of the first file into a data structure and then go about replacing the string. Finally write the new contents of the data-structure back to the same or a new file.

Its yr choice now... :)

Sriks
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top