Solved

Read TXT File

Posted on 2002-05-02
11
133 Views
Last Modified: 2010-04-04
If I have a file that’s look like this.

*****File******

0023fe12ceef;d2151
0023fe12f0ff;d2152
00c11098cce3;d2153

****End File******

And need to search for the first position before “;” and get the value after “;”


Ex.. I seach for 0023fe12f0ff and get d2152 as result.

I use no Form’s in my program so if you post example think of it….  
0
Comment
Question by:Dark_King
  • 3
  • 3
  • 2
  • +2
11 Comments
 
LVL 8

Accepted Solution

by:
Cesario earned 100 total points
ID: 6986052
HELLO Dark King,

try this code:

procedure TForm1.Button1Click(Sender: TObject);
var
  s : tstringlist;
begin
  s := tStringlist.create;
  s.loadfromfile ( 'c:\demo.txt');
  s.text := StringReplace(S.Text,';','=',[rfReplaceAll] );

  // now search for 0023fe12f0ff
  ShowMessage( S.Values['0023fe12f0ff']);

  // now test it with a rong value :
  ShowMessage( S.Values['x023fe12f0ff']);
  you will get an empty string

  s.free;
end;


Best Regards

Cesario
0
 
LVL 11

Expert Comment

by:robert_marquardt
ID: 6986056
A TStringList comes to mind.
If the file is not too big then load it with LoadFromFile.
after that sort the list. Setting sorted to True should be enough.
Then you can do a binary search for the index value.
If you replace the ';' by '=' you can use Names and Values of the TStringList to make it even easier.
0
 
LVL 11

Expert Comment

by:robert_marquardt
ID: 6986063
Ha, Cesario was faster, but sorting and binary search should be faster if you do much searching.
0
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
LVL 8

Expert Comment

by:Cesario
ID: 6986085
Hi Dark King,

so dont work with forms, so replace the Procedure with

Procedure FindItems;
.....
.....
End;

good luck
0
 

Expert Comment

by:jamesbooker
ID: 6986089
This is a very rough answer, but this method has served me well since I started Delphi Programming. It involves using for loops to get the beginning and after portions of a two-piece string.

-----------------------------

Function GetLeftSide(s:string): string;
var
rstring: string;
x: integer;
begin
   for x := 1 to (StrLen(Pchar(s))) do
      begin
         if s[x] = ';' then
            begin
               Result:=rstring;
               Break;
            end;
         rstring := rstring + s[x];
      end;
end; // GetLeftSide

Function GetRightSide(s:string): string;
var
rstring: string;
x: integer;
begin
   for x := (StrLen(Pchar(s))) downto 1 do
      begin
         if s[x] = ';' then
            begin
               Result:=rstring;
               Break;
            end;
         rstring := s[x] + rstring;
      end;
end; // GetRightSide

----------------
Both of these functions will return a string, and so first you read the next line of text from the file and pass it to the functions.

Ok so now you need to compare it. Lets have a look at the first line of your text file you posted.

0023fe12ceef;d2151

and you're searching through the file for the "0023fe12ceef" part of it. you read in the first line of the text file and say

if GetLeftSide(stringreadfromfile) = 'mySearchString' then
   myDestinationString := GetRightSide(stringreadfromfile)

This will see if the left-hand side of the current line in the file matches the one you are searching for. if it does, then
it puts the bit after the ; into the variable myDestinationString


Hope this helps
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 6986327
? why is hear an accepted and a proposed answer ?
0
 
LVL 6

Author Comment

by:Dark_King
ID: 6986340
Don't now
0
 
LVL 6

Author Comment

by:Dark_King
ID: 6986342
A Accepted Cesario Answer
0
 
LVL 8

Expert Comment

by:Cesario
ID: 6986352
cause jamesbooker proposed the Answer and Dark king gave me the points

Cesario
0
 

Expert Comment

by:jamesbooker
ID: 6986396
Agreed. Like I said, my answer was a little rough round the edges but I knocked it up from the top of my head without a compiler on this machine. The advantage of mine over Dark King's is that mine doesn't need the strings unit. His is a lot smaller though. :)
0
 
LVL 6

Author Comment

by:Dark_King
ID: 6987436
Sorry for not even try your example jamesbooker,
I already start to use Cesario

Cesario is it possible to use a second position.

0023fe12ceef;d2151;d215-1
0023fe12f0ff;d2152:d215-2
00c11098cce3;d2153;d215-3

And get to separate result.

Ex.. I search for  0023fe12f0ff and get d2152 and d215-2 in to String1 and String2
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Suggested Solutions

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…
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…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.

777 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