Solved

Text File Insert and Delete Capabilities

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

Enroll in June's Course of the Month

June's Course of the Month is now available! Every 10 seconds, a consumer gets hit with ransomware. Refresh your knowledge of ransomware best practices by enrolling in this month's complimentary course for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

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

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

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