0
votes

I have a record, and a file of records. I cant read the content of the file. I try to read from the file and save the data into a record called MiGuarde. When I try to print MiGuarde (and I HAVE data on my file), it shows nothing.

uses crt;

type GUARDERIA = record nombre, direccion : string[20];
                        total_caniles, cantidad_caniles, nro_mascota : integer;
                        valor_canil : real;
                        end;


     GU = file of GUARDERIA;


var eleccion : integer; G : GU;
    MiGuarde : GUARDERIA;


procedure CREAR_GUARDERIA;

begin

          assign(G,'C:\Users\MACIEL\Desktop\TP3 Algoritmos\GUARDERIAS.DAT');
          reset(G);

        if eof(G) then begin
                                 writeln('Ingrese el nombre de la Guarderia: ');
                                 readln(MiGuarde.nombre);
                                 writeln('Ingrese el total de caniles : ');
                                 readln(MiGuarde.cantidad_caniles);
                                 MiGuarde.nro_mascota := 0;
                                 writeln('Ingrese el valor por dia: ');
                                 readln(MiGuarde.valor_canil);
                                 writeln('Ingrese la direccion: ');
                                 readln(MiGuarde.direccion);

                                 write(G,MiGuarde);

                              end
                     else begin
                                       writeln('Ingrese el nuevo valor de estadia o "0" para salir');
                                 readln(eleccion);
                                 if eleccion > 0 then begin
                                                           MiGuarde.valor_canil := eleccion;
                                                           write(G,MiGuarde);
                                                      end;
                              end;

                              reset(G);
                              read(G,MiGuarde);
                              writeln(MiGuarde.nombre,'-',MiGuarde.cantidad_caniles);
                              readkey;

         close(G);

end;



begin


     repeat
            ClrScr;
            writeln('MENU');
            writeln();
            writeln('1. Generar guarderia (o modificar valor).');
            writeln('8. Salir.');
            writeln();
            writeln('- ');
            readln(eleccion);

            case eleccion of
            1 : CREAR_GUARDERIA;
            8 : exit;
            end;
       until eleccion = 8;
      readkey;

end.
1
Are you certain read(G,MiGuarde); etc actually executes? Btw what Pascal programming environment are you using? - MartynA
@MartynA: I suspect Turbo Pascal 3, because of TP3 Algoritmos\GUARDERIAS.DAT. - Rudy Velthuis

1 Answers

2
votes

Your (obviously incomplete) program has too many problems. For example, you do a Reset operation on the file, and then attempt to write to it -- which cannot possibly succeed. For writing you must open the file with Rewrite. It's possibly easier/safer to isolate the reading and writing operations into separate functions.

You do need to study Pascal a bit harder. Also, it would be easier to tell us what it's supposed to do as my Spanish[?] is hopeless.

You also need to organize your code so that it is more readable/manageable.

For example, you could create separate functions for reading and writing records. This will make your job easier. The following example is compatible to Freepascal / Turbo Pascal. (You also don't mention which Pascal compiler/dialect you're using.) I kept the record global to be closer to your original version, but you could also pass this as a parameter to these functions.

function ReadRec(n: Integer): Boolean;
begin
  ReadRec := False; // assume failure
  Assign(G,'data.dat');
  {$I-} Reset(G); {$I+}
  if IOResult <> 0 then exit;
  Seek(G,n);
  if not eof(G) then begin
    Read(G,MyStore);
    ReadRec := True;
  end;
  Close(G);
end;

function WriteRec(n: Integer): Boolean;
begin
  WriteRec := False; //assume failure
  Assign(G,'data.dat');
  {$I-} Rewrite(G); {$I+}
  if IOResult <> 0 then exit;
  Seek(G,n);
  Write(G,MyStore);
  Close(G);
  WriteRec := True;
end;

It's a bit hard to follow your code's logic. I suppose you are trying to create a database of many records, and not a single record. So, if many records, do you not need to also ask your user for the record number to work on when updating a record?

Below is my attempt to make your code functional and a bit more readable but without changing its current logic, which I'm not sure I fully follow, and it seems incomplete. Also, I Google-translated it to English hoping to better understand it.

At least it should prove (to you) that writing and reading records does work as it should.

type
  GUARDERIA = record
    nombre,
    direccion       : string[20];
    total_caniles,
    cantidad_caniles,
    nro_mascota     : integer;
    valor_canil     : real;
  end;
  GU = file of GUARDERIA;

var
  choice   : integer;
  G        : GU;
  MyStore  : GUARDERIA;

////////////////////////////////////////////////////////////////////////////////

procedure PrintRec;
begin
  with MyStore do begin
    Writeln('Name              ',nombre);
    Writeln('Address           ',direccion);
    Writeln('Number of pets    ',total_caniles);
    Writeln('Cantidad caniles  ',cantidad_caniles);
    Writeln('Number of pets    ',nro_mascota);
    Writeln('Num. of stay days ',valor_canil);
  end;
end;

////////////////////////////////////////////////////////////////////////////////

function ReadRec(n: Int64): Boolean;
begin
  ReadRec := False; // assume failure
  Assign(G,'data.dat');
  {$I-} Reset(G); {$I+}
  if IOResult <> 0 then exit;
  Seek(G,n);
  if not eof(G) then begin
    Read(G,MyStore);
    ReadRec := True;
  end;
  Close(G);
end;

////////////////////////////////////////////////////////////////////////////////

function WriteRec(n: Int64): Boolean;
begin
  WriteRec := False; //assume failure
  Assign(G,'data.dat');
  {$I-} Rewrite(G); {$I+}
  if IOResult <> 0 then exit;
  Seek(G,n);
  Write(G,MyStore);
  Close(G);
  WriteRec := True;
end;

////////////////////////////////////////////////////////////////////////////////

procedure InputRec;
begin
  Write('Enter the name          : ');
  Readln(MyStore.nombre);

  Write('Enter the number of dogs: ');
  Readln(MyStore.cantidad_caniles);

  MyStore.nro_mascota := 0;

  Write('Enter the number of days: ');
  Readln(MyStore.valor_canil);

  Write('Enter the address       : ');
  Readln(MyStore.direccion);
end;

////////////////////////////////////////////////////////////////////////////////

procedure AddRec;
begin
  if not ReadRec(0) then begin          //create first record if empty file
    InputRec;
    WriteRec(0);
  end;

  PrintRec;

  Writeln('Enter the stay days, or "0" to exit');
  Readln(choice);

  if choice > 0 then begin
    MyStore.valor_canil := choice;
    WriteRec(0);
  end;

  Writeln(MyStore.nombre,'-',MyStore.cantidad_caniles);
end;

////////////////////////////////////////////////////////////////////////////////

begin
  repeat
    Writeln('MENU');
    Writeln;
    Writeln('1. Create new record (or modify value)');
    Writeln('0. Exit');
    Writeln;
    Writeln('- ');
    Readln(choice);
    case choice of
      1 : AddRec;
      0 : Break;
    end;
  until False;
  Writeln('Bye');
end.

Hope this helps.