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

file manipulation

Status
Not open for further replies.

MarcDePoe

Programmer
Dec 19, 2000
101
US
Hello All,

OK...
There are two words ("building" and "office") scattered about a document. I must read this file into memory and in a loop, create a temp variable to hold the file contents so I can replace every instance of both "building" and "office" with a particular building or office [Sears Tower, etc.](merges).

The number of times to loop can be extremely great (mega loop) so it is very inefficient to do a replace every time. (there is no need to loop through the entire file every time just to locate these two words)

Instead, I am doing one scan of the file to record (into a variable) the location of every instance of both words (line # etc.)

Is there a way I can use these positions with pointers so that when inside the 'mega loop', I can go directly to the exact position of the word I aim to replace in the file, replace it, and proceed to the next instance and so on. (maybe seekg()?)

I know this would consume much less processor time and energy than some 'loop-through-every-char-in-the-file-each-time-replace-function'.

I'm developing this app with coldfusion and have pretty limited C knowledge, but any and all cf functions I've used are much too taxing on the processor, hence the need for something better and the posting of this thread.

Thanks,
Marc
 
Marc,

If I understand your 'problem/goal' correctly it sounds like disk IO is the real performance problem.

A basic technique to avoid becoming IO bound when saving changes to a file looks like this.

*Open the 'source' file.
*Open a 'temporary' file.
*Read a sizeable block of the source file into memory, something like 64k or 128k or whatever.
*Then search the block for the points that you want to modify the contents.
*Now there are a few different choices at this point.
**Modify the entire block in memory until all the data is corrected, either by growing the memory OR pre-allocating more than you need, OR use two memory buffers and write the changes into the second.
**Now you just write the entire 'correct' block into the 'temporary' file.

***Don't modify the memory just find the change points and write the buffer 'up to' the change point to the 'temporary' file... then write the change data. Repeate until the entire buffer is written to the 'temporary' file.

*Close both files
*Delete the old file
*Rename the temporary file to the old file name

Hope this helps
-pete
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top