Solved

Read TXT File

Posted on 2002-05-02
11
131 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
Comment Utility
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
Comment Utility
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
Comment Utility
Ha, Cesario was faster, but sorting and binary search should be faster if you do much searching.
0
 
LVL 8

Expert Comment

by:Cesario
Comment Utility
Hi Dark King,

so dont work with forms, so replace the Procedure with

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

good luck
0
 

Expert Comment

by:jamesbooker
Comment Utility
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 27

Expert Comment

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

Author Comment

by:Dark_King
Comment Utility
Don't now
0
 
LVL 6

Author Comment

by:Dark_King
Comment Utility
A Accepted Cesario Answer
0
 
LVL 8

Expert Comment

by:Cesario
Comment Utility
cause jamesbooker proposed the Answer and Dark king gave me the points

Cesario
0
 

Expert Comment

by:jamesbooker
Comment Utility
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
Comment Utility
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

743 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

15 Experts available now in Live!

Get 1:1 Help Now