Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Text File Insert and Delete Capabilities

Posted on 1997-09-07
1
Medium Priority
?
332 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 3

Accepted Solution

by:
mirek071497 earned 200 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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
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…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Suggested Courses

705 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