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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

editing file on the fly 1

Status
Not open for further replies.

biot023

Programmer
Nov 8, 2001
403
GB
Hallo.
Does anyone have any handy hints or pointers to handy hints as to how I could open a file for reading, locate a point in the file, and edit the characters there on the fly?
Usually, I'd open the file as an ifstream, read everything into a buffer, alter the buffer, then put it all back with alterations by writing it all back over the original with an ofstream.
But this time, I need a file that several users' applications are going to want to update, so it will need to be opened in protect mode. I don't want other users with an update pending to have to hang about for ages (there will be alot of information in this file), so it would be best if I could make the update as fast as possible.
Any help gratefully received.

Cheers,
Douglas JL

PS - did anybody clock my threads on sockets & TClient socket?
Common sense is what tells you the world is flat.
 
Well, the thing is to find the location to alter/read, if You know that it's just opening the file, use FileSeek(Index) to position and if You are to alter say 4 bytes You have those in a buffer and write them with the WriteFile(...) function and then close the file! Done!
IF You however has to search through i would solve it like this: Reserve memory, maybe the full size of the file or lesser portions, here we go for lesser portions. Read in a hunk of the file into buffer with ReadFile(...) and search the datas for the pattern to recognize, if found do the alteration and save that hunk of memory back into file and exit. Otherwise (not found) read next hunk, re-do and so forth. Small hunks could lead to long search times, big hunks can lead to long read/write times, find a balance. Totte
 
I forgot one thing: If You are to alter the size of the datas i.e. change "his" to "hers" (3 to 4 char's) it's a little more tricky, then you has to rewrite the rest of the file alltogether. Totte
 
Thanks - I'd set aside a number of chars per bit of data, and fill any excess with whitespace - that way, once I'd found my bit, I could write over it like it was a type.
Cheers,
Douglas JL Common sense is what tells you the world is flat.
 
I would suggest that you use asterisks or some other similar to fill in any unused space.
 
Yeah - that's probably more sensible, cheers. Common sense is what tells you the world is flat.
 
One thing though regarding whitespace:
If it's text Your'e replacing then use the whitespace NULL, this allows any strlen() and such to work perfect.....as ASCII(0) is used at standard C End_Of_String. Totte
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top