Solved

Advice for processing strings/text?

Posted on 1997-12-15
4
131 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

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

Title # Comments Views Activity
Simple Delphi Question 9 83
Communication Between RC4 Delphi <-> PHP 3 100
How to open a new windows chrome resized and disabled the toolbar? 3 82
CheckListBox usage 3 51
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

895 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