Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Reading a text file

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
Hi,

I am writing a program that needs to read a text file such as the following,

[Region]
0=Japan,0000C2,0
1=USA,0000C2,2
2=Hispanic,0000C2,4
3=Asia,0000C2,6
4=Euro,0000C2,8

[0]
Name=P1 Max Exp
0=OFF,0,0
1=ON,FF86EE,99,FF86EF,9,FF86F0,99,FF86F1,99

[1]
Name=P1 Inf. SP
0=OFF,0,0
1=ON,FF86F2,9,FF86F4,99,FF86F5,99

If we look at the bottom section I would like to read the "1","P1 Inf. SP","Off","0","0","ON","FF86F2","9","FF86F4","99","FF86F5","99" into different strings. What is the easiest way to do this given that the length of all these strings can vary - they will always be seperated by a comma.

Also, is it possible to convert a string into a PChar that can be used with ptr() and the ^.

Thanks for any help,

Barry
 
My favorite parsing function... you must pass in a valid stringlist pointer for the tokens parameter.

{ParseStringQual}
procedure ParseStringQual(str, delimiter, qualifier: string; tokens: TStringList);
var token: string;
x: Longint;
qualified: Boolean;
begin
tokens.Clear;
qualified := False;
for x := 1 to Length(str) do
begin
if (token='') and (str[x]=qualifier) and not(qualified) then
begin //we're at the beginning of a token, and it's qualified
qualified := True;
end
else if (str[x] = qualifier) and qualified then
begin //this is the ending qualifier, next delimiter signifies end
qualified := False;
end
else if (str[x] = delimiter) and not(qualified) then
begin //we're not between qualifiers and we found a delimiter
tokens.Add(Trim(token));
token := '';
end
else
begin
token := token + str[x];
end;
end;
tokens.Add(trim(token));
end;

Hope this helps,
TealWren
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top