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
Solved

Advice for processing strings/text?

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

789 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