1
votes

Program:

  program s;

  type info = record
                    name, surname: string;
                    min, sek: integer;
              end;

  type arrays = array[1..50] of info;

  var c, b: text;
  A: arrays;
  gr_sk,
  grup_dal: integer;

  begin

  assign(c, 'info.txt'); reset(c);
  read(c, gr_sk);
  read(c, grup_dal);
  id := 1;

  read(c, A[id].name);
  read(c, A[id].sek);

  close(c);

end.

info.txt file:

3
4
yhgf
4

Please, tell me what is wrong with that. It says that it is bad number format for line 19 I guess.

If I change min, sek: integer; to min, sek: string; then it works. So as I understand, it reads number like string. How can it be, I have never experienced that before. God, I will shoot myself.

3
Donator, your c is text. That way you read only "string" data from the file. - Nick Dandoulakis
You are wrong, that's not true. - good_evening

3 Answers

0
votes

This is what i think

You are trying to read 'yhgf' into an integer (gr_sk), so when read reads, it throws an error because 'yhgf' can't be transformed into an integer.

What should you do?

Well, I think that you can read it into a string, validate that it is a number, and then transform it into an integer. Frankly, I don't remember the Pascal way to do it. After googling around, found val procedure.

Val converts the integer or real number that is represented by the characters in the string Source and places it into x.

Some tips on string functions / procedures:

0
votes

I have many years to use old Pascal (nowadays I use Delphi), and I think you are using your c variable wrongly. Anyway, in Pascal you can declare file variables and associate a record type on them.

Example:

  type info ...

  var f: file info; { <-- here }
  ...

  begin
     assign(f, 'info.txt');
     reset(f);
     id := 1;
     read(f, A[id]);
     close(f);
  { now A[1] should contain file data }
  ...
0
votes

Change

read(c, A[id].name);

to

readln(c, A[id].name);

Anyway I would search the problem in that direction; namely the not reading of the lineseparator. (CR and LF)

When in doubt, do a read(f,) and write the ORD() of a few of the read chars to screen