[fpc-pascal]opening files from a text list

Anton Tichawa anton.tichawa at chello.at
Fri Nov 28 11:16:34 CET 2003


Hello!

>    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.
>    Some teams have the same initial, so just reading the first character
> fails to provide enough uniqueness. I know I could set up a complex set of
> embedded case statements (case first char -> case second char), but
> there's surely an easier way? That method would leave the team names
> hard-coded into the program, which isn't a very good way to do things.
>

You need string manipulation. Here is a simple example, dividing the line 
into "black" tokens separated by "white" spaces (it gets more complicated 
when dealing with tabs, numbers etc.):

program football;

var
  file0: text;
  line: string;
  away: string;
  home: string;

function scanblack(var s: string): string;
var
  i: integer;
begin
  i := 1;
  repeat
    if i > length(s) then begin
      break;
    end;
    if s[i] = ' ' then begin
      break;
    end;
    inc(i);
  until false;
  result := copy(s, 1, i - 1);
  delete(s, 1, i - 1);
end;

function scanwhite(var s: string): string;
var
  i: integer;
begin
  i := 1;
  repeat
    if i > length(s) then begin
      break;
    end;
    if s[i] <> ' ' then begin
      break;
    end;
    inc(i);
  until false;
  result := copy(s, 1, i - 1);
  delete(s, 1, i - 1);
end;

begin
  assign(file0, '49ers');
  reset(file0);
  readln(file0, line);
  close(file0);
  scanwhite(line);
  away := scanblack(line);
  scanwhite(line);
  if scanblack(line) <> 'at' then begin
    writeln('syntax error');
    halt;
  end;
  scanwhite(line);
  home := scanblack(line);
  writeln('AWAY: ', away, '; HOME: ', home);
end.

hth,

Anton.

----------

Ing. Anton Tichawa
Volkertstrasse 19 / 20
A-1020 Wien
phone: +43 1 218 97 59
email: anton.tichawa at chello.at

----------

"Adas Methode war, wie sich zeigen wird, Tagträume in offenbar korrekte 
Berechnungen einzuweben."

Doris Langley Moore: Ada, Countess of Lovelace (London 1977).

----------





More information about the fpc-pascal mailing list