Solved

Advice for processing strings/text?

Posted on 1997-12-15
4
130 Views
Last Modified: 2010-04-04
I am just interested in some advice on this one, as many times I have done something new in Delphi only to find out later I could have done it alot easier! :-)
I am looking for advice on processing a text file. What I plan on doing is reading a text file, parsing it line by line, and char by char, and updating this same text file.
I may be removing lines, adding lines, or removing chars in the line.  Because I really haven't done much text file processing like this in Delphi, my initial plans are to open the file, read the the line into an array, parse through it, and rewrite this line back to the file (or maybe just ignore it and get the next line).
I understand this is a brief explanation, but I am assuming that reading each line into an array an parsing through the array is the best way in Delphi.  Can someone point me in the right direction on this, and is there some other way which I should research before doing it this way?

             Thanks a bunch
0
Comment
Question by:jexd99
  • 2
4 Comments
 
LVL 8

Expert Comment

by:ZifNab
ID: 1353734
What about reading it in a memo, and do all your stuff in there?
Like adding, deleting, ... lines, chars, ... .
You can create a memo at run-time or you place an invisible memo component on the form.
Don't know if this is the best way, just an idea (Normally I would do it like you, but the memo has already some handy functions/procedures)
0
 
LVL 8

Expert Comment

by:ZifNab
ID: 1353735
Like this :

 var MyText : TMemo;

 begin
  MyText := TMemo.Create(self);
  with MyText do begin
   Clear;
   Lines.LoadFromFile('HereNameOfFile');
   { Do your stuff }
     // e.g.
     // delete line 3
      Lines.Delete[2]
     // add a line
      Lines.Add('I add this line');
    { end }
    { save the result to a file }
    Lines.SaveToFile('NameOfFile');
  end;
  MyText.Free;
  MyText := nil;
 end;
 

0
 
LVL 3

Accepted Solution

by:
Pegasus100397 earned 30 total points
ID: 1353736
Jex,

  Zif has a good answer and allows you to see what is happening but if I just wanted something quick & dirty, no-frills, grind-the file parsing doohicky I'd just open the input file (source) and an output file (destination) read the stuff in line by line chopping, hacking, etc and write out to the second file what I wanted to keep. And then delete the source file, and rename the "Destination" file to the same name as the previously-deleted source file..

Good luck with your project!
Pegasus
0
 

Author Comment

by:jexd99
ID: 1353737
Thanks to both of you. These are a couple of ideas I was looking for. I was considering the delete/rename option, and I'm not real familiar with the memo comonent, but I'll dig into it.  It seems like everytime I do something new I find out 5 months later that I could have done it a diff way with 1/2 the code! I just wanted to make sure I wasn't missing something this time around. :-)
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
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…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

758 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

19 Experts available now in Live!

Get 1:1 Help Now