Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Reading strings

Posted on 2004-05-02
5
Medium Priority
?
298 Views
Last Modified: 2010-04-05
I have this code that opens a text file and reads every line and looks for $ symbol
then it reads bacwards until it finds a space and foreward the same way, and copys the string in between the spaces  how can i change it to read backwards until it finds @ and foreward until it finds a @  symbol?

is this the right way, or do i need to use blockread ?

var
  r : TextFile;
  F : TextFile;
  S : STRING;
  i : integer;
begin

    AssignFile(F,File1);
  Reset(F);
   REPEAT
  // REPEAT
   Readln(F,S);
 I := Pos('$',S);
 if I > 0 then begin
   while (I > 0) and (S[I] <> '@ ') do Dec(I);
   S := Copy(S,I + 1,Length(S) - I);
   I := Pos('@ ',S + '@ ');
   A := Copy(S,1,I - 1);
    // S := Copy(S,I,Length(S) - I + 1);
 end;
   Writeln(r,a);
 // showmessage(a);
 //UNTIL (i)=0;
 UNTIL Eof(F);
 CloseFile(F);
 end;
0
Comment
Question by:herd_bone
[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
  • 3
  • 2
5 Comments
 
LVL 12

Expert Comment

by:esoftbg
ID: 10974933
procedure TForm1.SpeedButton1Click(Sender: TObject);
var
  r : TextFile;
  f : TextFile;
  A : STRING;
  S : STRING;
  I : integer;
  J : integer;
  K : integer;
begin
  J := 0;
  AssignFile(r, 'Q_20975533_r.txt');
  Rewrite(r);
  AssignFile(F, 'Q_20975533.txt');
  Reset(f);
  repeat
    Readln(f, S);
    I := Pos('$', S);
    if I > 0 then
    begin
      J := I;
      while (J > 0) and (S[J] <> '@') do
        Dec(J);
      K := I;
      while (K < Length(S)) and (S[K] <> '@') do
        Inc(K);
      if (J>0) then
      begin
        A := Copy(S, J+1 , K-J-1);
        S := Copy(S, K, Length(S) - K + 1);
      end;
    end;
    if (J>0) then
      Writeln(r, A);
  until Eof(f);
  CloseFile(r);
  CloseFile(f);
end;
0
 

Author Comment

by:herd_bone
ID: 10975221
It works good, one question though, why does it put too many of the same strings in the list
if i read a file that has alot of lines, it puts duplicate strings into my result..
0
 
LVL 12

Expert Comment

by:esoftbg
ID: 10975321
I did try a simple text file wich contains 3 rows and only the 3-d contains '@ and foreward $ until it finds a @':

I have this code that opens a text file and reads every line and looks for $ symbol
then it reads bacwards until it finds a space and foreward the same way, and copys the string in between the spaces  
how can i change it to read backwards until it finds @ and foreward $ until it finds a @  symbol?

I guess my code is not perfect. I need to try with more complex text file.
emil
0
 
LVL 12

Accepted Solution

by:
esoftbg earned 440 total points
ID: 10975395
try this, I tested with more complex text file:

procedure TForm1.SpeedButton1Click(Sender: TObject);
var
  r : TextFile;
  f : TextFile;
  A : string;
  S : string;
  I : integer;
  J : integer;
  K : integer;
begin
  AssignFile(r, 'Q_20975533_r.txt');
  Rewrite(r);
  AssignFile(F, 'Q_20975533.txt');
  Reset(f);
  repeat
    Readln(f, S);
    I := Pos('$', S);
    while (I > 0) do
    begin
      J := I;
      while (J > 0) and (S[J] <> '@') do
        Dec(J);
      K := I;
      while (K < Length(S)) and (S[K] <> '@') do
        Inc(K);
      if (J>0) then
      begin
        A := Copy(S, J+1 , K-J-1);
        Writeln(r, A);
      end;
      S := Copy(S, K+1, Length(S) - K + 1);
      I := Pos('$', S);
    end;
  until EOF(f);
  CloseFile(r);
  CloseFile(f);
end;
0
 

Author Comment

by:herd_bone
ID: 10980942
Thanks esoftbg.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Suggested Courses

730 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