Solved

Text File Insert and Delete Capabilities

Posted on 1997-09-07
1
329 Views
Last Modified: 2010-04-04
Is there a way to insert and delete data in an open text file without having to rewrite the entire text file.

Consider for example that I've open a text file, located a specific string within the text file (line, position and length) and need to either remove the selected string from the file or replace it with another string.  

The method I'm currently using to (for example) replace a string seems very inefficient:

1. Locate the string (search method)
2. Read and store all data  upto the string of interest in  array 1.
3. Read and store all data following the string of interest in  array 2.
4. Create a temp file
5. Write all data from array 1 to the temp file, write the replacement string to the temp file, then write all data in array 2 to the temp file.
6. Destroy the original file.
7. Rename the temp file to the original file name.

This process if very cumbersom especially if I only need to change one character in a 5000 line text file.  There has to be a better way.

Thanks
 James Tallent
0
Comment
Question by:scitech
1 Comment
 
LVL 3

Accepted Solution

by:
mirek071497 earned 100 total points
ID: 1344300
No!
If you need text file then no other way, but you can construct 2 files. One with indexes (positions of strings) and the second with strings. You can then mark as deleted without real deletion or exchange strings with exchange only indexes.
If you have for example 10000 lines of text then index have 10000*sizeof(Longint) and you can cache it in memory for very fast operation.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

861 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question