Solved

Text File Insert and Delete Capabilities

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
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…
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.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

929 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

11 Experts available now in Live!

Get 1:1 Help Now