herd_bone
asked on
Reading strings
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;
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;
ASKER
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..
if i read a file that has alot of lines, it puts duplicate strings into my result..
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
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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks esoftbg.
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;