[fpc-pascal]opening files from a text list
ZINTEL Gerhard
gzintel at stadeln.faurecia.com
Fri Nov 28 13:03:41 CET 2003
Hello Donald,
> If I have a file which contains on the first line...
>
> 49ers at Giants 16 13
>
> I want to open a file called "49ers" and a file called
> "Giants" and do
> some processing. Although FP handles strings, I believe that
> you can still
> only read a character at a time, so I don't know how to read
> "49ers" into
> a variable and open that file. The variable would be called
> Away say, so
> how do I get "49ers" into Away so that I can then do
> assign(awayteam,Away)? I would then want to read "Giants"
> into Home, and
> do assign(hometeam,Home), and go from there.
here is just another approach. The function NextToken scans a string for the
next token and gives it back. At the same time it shortens the original
sting. So with each call of it you'll get the next token available. It
handles blanks and tabs as white space character and is easily extendible
for other "white space" characters as ':', ';', ...
Here is the function with a short program for testing.
program TestNextToken;
var s : string;
{-----------------------------------------------------------------------}
function nextToken(var s:string):string;
{ reads next token from string s and shortens s by this amount
}
{-----------------------------------------------------------------------}
const whitespace = [' ',#9]; // whitespace should be blank and tab
var l, start, stop : integer;
begin
l := length(s);
start := 1;
// searching first non whitespace character
while (start <= l) and (s[start] in whitespace) do inc(start);
stop:=start;
// searching last non whitespace character
while (stop <= l) and (not (s[stop] in whitespace)) do inc(stop);
if s[stop] in whitespace then dec(stop);
nextToken := copy(s,start,stop-start+1);
delete(s,1,start+stop-1);
end;
begin
s := ' aaa bbb ccc '; // a test string
writeln(nextToken(s));
writeln(nextToken(s));
writeln(nextToken(s));
end.
In doing it this way is a bit more obvious, at least for me.
Gerhard
Gerhard Zintel
Faurecia Abgastechnik GmbH
Abt.: EGA
Fon.: +49 911 7610171
Fax.: +49 911 7610350
e-mail: GZintel<at>Stadeln.Faurecia.com
More information about the fpc-pascal
mailing list