Solved

Advice for processing strings/text?

Posted on 1997-12-15
4
146 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
[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
  • 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…

734 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