Solved

Text File Insert and Delete Capabilities

Posted on 1997-09-07
1
326 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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

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…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

707 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now