0
votes

1.this is my code i want to read a record from a text file into array in pascal my program is about making a hotel helper and i already have a text file with the data of the hotel then i should read it from the text file and store it in array .. but i am facing error 103 exit code (file not open).... any help Please . :)

program Hotel1(input,output);

const  max =10;  MaxFloor =10;
type
Date = record
        day :1..31;
        month:1..12;
        year:integer;
       end;


Booking = record
           Guest:string[20];
           S_Date:date;
           E_date:date;
          end;


Booking_Mat= array[1..max] of Booking;

History_Booking = record
                   B_num:integer;
                   B_Mat:Booking_Mat;
                  end;

Room = record
        Num:integer;
        Bed_num:integer;
        Price:integer;
        Status:Boolean;
        H:History_Booking;
       end;

Data = record
       Ro:Room;
       m:integer;
       end;

Data_mat= array [1..max] of Data;



Procedure  Read_Data(filename:string; var table:Data_mat);
 var   df:text; i,j :integer;
  n,m,num,GN:integer;
  Bed_num,Price:integer;
  f:text;
  s,e:Date;
  Gname:string[20];
  ok:boolean;
  a:Data_mat;
 c:char;
 Begin
 writeln('Reading ',filename,' records into array.... ');
 assign(df,filename);
 reset(df);
i:=0;
   while (not eof) do
     begin
        i:=i+1;
         Read (f,num);
         a[i].Ro.num:=num;
         Read (f,Bed_num);
         a[i].Ro.Bed_num:=Bed_num;
         Read (f,Price);
      a[i].Ro.Price:=Price;
         Read(f,c);
          if (c ='Y') then
           a[i].Ro.status:= true
       else
           a[i].Ro.status:= false;

      readln;

  End; {while eof}
  close(df);
End; {Read_Data}
1
in the Main program i am just calling the procedure (Read_Data) - luna
Loong Time since I did this, but shouldn't it Read have df as it's first argument, that's your file. - Tony Hopkinson

1 Answers

0
votes

You've declared two variables of type Text, (df and f) in your var block.

You open df with these lines:

assign(df,filename);
reset(df);

You then read from f (which is not the file you opened above) in several lines, such as this one:

Read (f, num);

It's interesting to note that you actually manage to close the file you really opened, even though you never use it in your loop:

close(df);

The solution to all of these issues is to delete the declaration of either f or df, and then fix the compiler errors you get by correcting the code to use the remaining text variable. (Two important lessons here are

  1. Only declare the variables you actually need.
  2. Use the variables you declare.

Your loop is also invalid, because you're using while not eof with no file provided for which to test the end. Your loop should read while not Eof(df) do instead.

It's also much better to follow the typical naming convention of prefixing types with a T. It makes it clear that it's a type and not a variable, and allows you to read the code more easily. For instance, I'd change your definition of Data to TRoomData, and change the other type declarations accordingly. Here's an example - note that TRoomData now has a field (member) named Room of type TRoom:

TRoomData = record
  Room: TRoom;
  m: Integer;
end;

TRoom is defined as

TRoom = record
  Num: Integer;
  Bed_num: Integer;
  Price: Integer;
  Status: Boolean;
  H: THistory_Booking;
end;

And so forth. This allows you to write code more clearly:

var
  RoomData: TRoomData;
begin
  RoomData.Room.Num := 1;
  RoomData.Room.Price := 50;
  // etc.
end;

With all that being said, your file does not contain text, and therefore you're using the wrong file type by using df: Text in the first place. You should use a File of TRoomData, allowing you to read and write entire records at a time. Here's an example of doing so:

var
  DF: File of TRoomData;
  RoomData: TRoomData;
  i: Integer;
const
  DataFileName = 'D:\TempFiles\RoomData.dat';

Writing it:

// Put some data into the record
RoomData.Room.Num := 1;
RoomData.Room.Bed_num := 1;
RoomData.Room.Price := 40;
RoomData.Room.Status := True;
RoomData.Room.H.B_num := 1;
for i := 1 to Max do
begin
  RoomData.Room.H.B_Mat[1].Guest := Format('Guest %d', [i]);
  RoomData.Room.H.B_Mat[1].S_Date.Year := 2014;
  RoomData.Ro.H.B_Mat[1].S_Date.Month := i;
  RoomData.Ro.H.B_Mat[1].S_Date.Day := i;
end;

// Write it out to the file
AssignFile(DF, DataFileName);
try
  Rewrite(DF);
  Write(DF, RoomData);
finally
  CloseFile(DF);
end;

Reading it back in:

AssignFile(DF, DataFileName);
try
  Reset(DF);
  Read(DF, RoomData);
finally
  CloseFile(DF);
end;

(Or, better yet: If the version of Pascal you're using supports it, move away from the old file I/O routines and start using TFileStream instead.)

Last but not least, learn to properly format your code. It makes it much easier to debug and maintain, and it's much easier to read when you can follow the execution path clearly.