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

Read TXT File

Posted on 2002-05-02
11
134 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
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

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…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

792 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