Lazarus

Free Pascal => Beginners => Topic started by: manahu on February 27, 2021, 09:44:47 pm

Title: File handling, records [pascal]
Post by: manahu 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
Title: Re: File handling, records [pascal]
Post by: jamie 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
Title: Re: File handling, records [pascal]
Post by: Kays 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 (https://wiki.freepascal.org/Read) and write (https://wiki.freepascal.org/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 (https://wiki.freepascal.org/File_Handling_In_Pascal) in the Wiki.
Title: Re: File handling, records [pascal]
Post by: manahu 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!
Title: Re: File handling, records [pascal]
Post by: manahu 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 (https://wiki.freepascal.org/Read) and write (https://wiki.freepascal.org/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 (https://wiki.freepascal.org/File_Handling_In_Pascal) in the Wiki.

you were right, the file was empty. Thanks!
TinyPortal © 2005-2018