Recent

Author Topic: File handling, records [pascal]  (Read 1235 times)

manahu

  • Newbie
  • Posts: 3
File handling, records [pascal]
« on: February 27, 2021, 09:44:47 pm »
Hi, I have a problem when reading a file, It contains records,
     this is the code:

Code: Pascal  [Select][+][-]
  1.  
  2. program archivos2;
  3. uses
  4.         crt;
  5. type
  6.         jugadores = record
  7.                 nombre:string[10];
  8.                 edad:string[10];
  9.         end;
  10.  
  11.         fJugadores = file of jugadores;
  12. var
  13.         conjuntoJugadores:fJugadores;
  14.         unicosJugadores:jugadores;
  15.         justString:string;
  16.         continuar:char;
  17.         x:integer;
  18. begin
  19.         assign(conjuntoJugadores,'listadoJugadores.txt');
  20.         rewrite(conjuntoJugadores);
  21.  
  22.         with unicosJugadores do
  23.                 repeat
  24.                         write('nombre: ');
  25.                         readln(nombre);
  26.                         write('Edad: ');
  27.                         readln(edad);
  28.  
  29.                         write('desea ingresar otro jugador s/n: ');
  30.                         continuar := upcase(readkey);
  31.  
  32.                         until continuar = 'N';
  33.  

until here works just fine, but I can't read It

Code: Pascal  [Select][+][-]
  1.  
  2. reset(conjuntoJugadores);
  3.  
  4.        while not eof (conjuntoJugadores) do
  5.        begin
  6.                         read(conjuntoJugadores,jugadores);
  7.                         write(jugadores);
  8.        end;
  9.  
  10. close(conjuntoJugadores);
  11. end.
  12.  
  13.  

error I get is can't read or write variable of this type

jamie

  • Hero Member
  • *****
  • Posts: 6077
Re: File handling, records [pascal]
« Reply #1 on: February 27, 2021, 10:38:43 pm »
you need to use the Instance to fill data in not the type.
replace jugadores with unicosJugadores
 for the READ(….) and write problem areas
The only true wisdom is knowing you know nothing

Kays

  • Hero Member
  • *****
  • Posts: 569
  • Whasup!?
    • KaiBurghardt.de
Re: File handling, records [pascal]
« Reply #2 on: February 27, 2021, 11:02:24 pm »
Hi, I have a problem when reading a file, It contains records,
No, it does not: I don’t see your repeat…until loop actually containing a
Code: Pascal  [Select][+][-]
  1. write(conjuntoJugadores, unicosJugadores);

[…] error I get is can't read or write variable of this type
The problem’s what jamie just wrote. It must read
Code: Pascal  [Select][+][-]
  1. read(conjuntoJugadores, unicosJugadores);
  2. write(unicosJugadores.nombre, unicosJugadores.edad); // print to `output`
The first parameter of read and write are the source or destination respectively (input and output can be automatically supplemented) and the following parameters are variables for read and expressions for write.

You have accidentally specified the name of a data type, though. See also File handling in Pascal in the Wiki.
Yours Sincerely
Kai Burghardt

manahu

  • Newbie
  • Posts: 3
Re: File handling, records [pascal]
« Reply #3 on: February 28, 2021, 02:16:48 am »
you need to use the Instance to fill data in not the type.
replace jugadores with unicosJugadores
 for the READ(….) and write problem areas

Thanks a lot, It works now!

manahu

  • Newbie
  • Posts: 3
Re: File handling, records [pascal]
« Reply #4 on: February 28, 2021, 02:19:59 am »
Hi, I have a problem when reading a file, It contains records,
No, it does not: I don’t see your repeat…until loop actually containing a
Code: Pascal  [Select][+][-]
  1. write(conjuntoJugadores, unicosJugadores);

[…] error I get is can't read or write variable of this type
The problem’s what jamie just wrote. It must read
Code: Pascal  [Select][+][-]
  1. read(conjuntoJugadores, unicosJugadores);
  2. write(unicosJugadores.nombre, unicosJugadores.edad); // print to `output`
The first parameter of read and write are the source or destination respectively (input and output can be automatically supplemented) and the following parameters are variables for read and expressions for write.

You have accidentally specified the name of a data type, though. See also File handling in Pascal in the Wiki.

you were right, the file was empty. Thanks!

 

TinyPortal © 2005-2018