Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

What's wrong?

mariblomariblo Posts: 4Member
Can someone please help me to find out why the filehandling doesn't work? If you need to see the rest of the subroutines I will post them too. Thank you!!!

[Code] PROGRAM inlupp6(Input, Output);

CONST
kundmax = 100;
kreditmax = 5000;

TYPE
kundpektyp = ^transaktionstyp;
string_30 = PACKED ARRAY[1..30] of Char;
string_12 = PACKED ARRAY[1..12] of Char;
kundtyp = RECORD
namn : string_30;
gadress : string_30;
padress : string_30;
tele : string_12;
saldo : Integer;
kredit : Integer;
first : kundpektyp;
END; {kundtyp}
transaktionstyp = RECORD
datum : string_12;
transtyp : string_12;
transaktion : Integer;
next : kundpektyp;
END; {transaktionstyp}
regtyp = RECORD
kundarr : ARRAY[1..kundmax] of kundtyp;
antal : Integer;
END; {regtyp}
kundfil = FILE of kundtyp;
transfil = FILE of transaktionstyp;

VAR
kom, svar : char;
kundreg : regtyp;

PROCEDURE loadtrans(VAR kunden : regtyp);
VAR
tfil : transfil;
ny, sist : kundpektyp;
i : Integer;
BEGIN
RESET(tfil, 'transaktion.dat');
FOR i:=1 TO kunden.antal DO BEGIN
IF kunden.kundarr[i].first<>NIL THEN BEGIN
kunden.kundarr[i].first:=NIL;
REPEAT
New(ny);
Read(tfil, ny^);
IF kunden.kundarr[i].first=NIL THEN
kunden.kundarr[i].first:=ny
ELSE
sist^.next:=ny;
sist:=ny;
UNTIL ny^.next=NIL;
END; {IF}
END; {FOR}
END; {ladda}

PROCEDURE loadkund(VAR kunden : regtyp);
VAR
kfil : kundfil;
BEGIN
RESET(kfil, 'kundreg.dat');
kunden.antal:=0;
WHILE NOT Eof(kfil) DO BEGIN
kunden.antal:=kunden.antal+1;
Read(kfil, kunden.kundarr[kunden.antal]);
loadtrans(kunden);
END; {WHILE}
END; {loadkund}

PROCEDURE savekund(VAR kunden : regtyp);
VAR
fil1 : kundfil;
fil2 : transfil;
temp : kundpektyp;
i : Integer;
BEGIN
Rewrite(fil1, 'kundreg.dat');
Rewrite(fil2,'transaktion.dat');
FOR i:=1 TO kunden.antal DO BEGIN
Write(fil1, kunden.kundarr[i]);
temp:=kunden.kundarr[i].first;
WHILE temp<>NIL DO BEGIN
Write(fil2, temp^);
temp:=temp^.next;
END; {WHILE}
END; {FOR}
END; {savekund}

BEGIN {HP}
Writeln('Welcome! ');
Writeln('Do you want to start? Y/N ');
Readln(svar);
svar:= versal(svar);
IF (svar='Y') THEN
loadkund(kundreg);
REPEAT
readkom(kom);
CASE kom OF
'N' : donykund(kundreg);
'T' : dotrans(kundreg);
'S' : skriv_trans(kundreg);
'Q' : ;
END; {CASE}
UNTIL kom='Q';
savekund(kundreg);
END.
[/CODE]

Comments

  • zibadianzibadian Posts: 6,349Member
    : Can someone please help me to find out why the filehandling doesn't work? If you need to see the rest of the subroutines I will post them too. Thank you!!!
    :
    : [Code] PROGRAM inlupp6(Input, Output);
    :
    : CONST
    : kundmax = 100;
    : kreditmax = 5000;
    :
    : TYPE
    : kundpektyp = ^transaktionstyp;
    : string_30 = PACKED ARRAY[1..30] of Char;
    : string_12 = PACKED ARRAY[1..12] of Char;
    : kundtyp = RECORD
    : namn : string_30;
    : gadress : string_30;
    : padress : string_30;
    : tele : string_12;
    : saldo : Integer;
    : kredit : Integer;
    : first : kundpektyp;
    : END; {kundtyp}
    : transaktionstyp = RECORD
    : datum : string_12;
    : transtyp : string_12;
    : transaktion : Integer;
    : next : kundpektyp;
    : END; {transaktionstyp}
    : regtyp = RECORD
    : kundarr : ARRAY[1..kundmax] of kundtyp;
    : antal : Integer;
    : END; {regtyp}
    : kundfil = FILE of kundtyp;
    : transfil = FILE of transaktionstyp;
    :
    : VAR
    : kom, svar : char;
    : kundreg : regtyp;
    :
    : PROCEDURE loadtrans(VAR kunden : regtyp);
    : VAR
    : tfil : transfil;
    : ny, sist : kundpektyp;
    : i : Integer;
    : BEGIN
    : RESET(tfil, 'transaktion.dat');
    : FOR i:=1 TO kunden.antal DO BEGIN
    : IF kunden.kundarr[i].first<>NIL THEN BEGIN
    : kunden.kundarr[i].first:=NIL;
    : REPEAT
    : New(ny);
    : Read(tfil, ny^);
    : IF kunden.kundarr[i].first=NIL THEN
    : kunden.kundarr[i].first:=ny
    : ELSE
    : sist^.next:=ny;
    : sist:=ny;
    : UNTIL ny^.next=NIL;
    : END; {IF}
    : END; {FOR}
    : END; {ladda}
    :
    : PROCEDURE loadkund(VAR kunden : regtyp);
    : VAR
    : kfil : kundfil;
    : BEGIN
    : RESET(kfil, 'kundreg.dat');
    : kunden.antal:=0;
    : WHILE NOT Eof(kfil) DO BEGIN
    : kunden.antal:=kunden.antal+1;
    : Read(kfil, kunden.kundarr[kunden.antal]);
    : loadtrans(kunden);
    : END; {WHILE}
    : END; {loadkund}
    :
    : PROCEDURE savekund(VAR kunden : regtyp);
    : VAR
    : fil1 : kundfil;
    : fil2 : transfil;
    : temp : kundpektyp;
    : i : Integer;
    : BEGIN
    : Rewrite(fil1, 'kundreg.dat');
    : Rewrite(fil2,'transaktion.dat');
    : FOR i:=1 TO kunden.antal DO BEGIN
    : Write(fil1, kunden.kundarr[i]);
    : temp:=kunden.kundarr[i].first;
    : WHILE temp<>NIL DO BEGIN
    : Write(fil2, temp^);
    : temp:=temp^.next;
    : END; {WHILE}
    : END; {FOR}
    : END; {savekund}
    :
    : BEGIN {HP}
    : Writeln('Welcome! ');
    : Writeln('Do you want to start? Y/N ');
    : Readln(svar);
    : svar:= versal(svar);
    : IF (svar='Y') THEN
    : loadkund(kundreg);
    : REPEAT
    : readkom(kom);
    : CASE kom OF
    : 'N' : donykund(kundreg);
    : 'T' : dotrans(kundreg);
    : 'S' : skriv_trans(kundreg);
    : 'Q' : ;
    : END; {CASE}
    : UNTIL kom='Q';
    : savekund(kundreg);
    : END.
    : [/CODE]
    :
    :
    If you are using TP, you first neet to call assign() before calling the reset() and after you have read/written the data, you need to call close(). Example:
    [code]
    var
    f: file of char;
    begin
    Assign(f, 'test.dat'); { Connect the file variable to a filename }
    Rewrite(f); { Open the file for writing }
    write(f, 'b', 'l', 'a');
    Close(f); { Close the file connection, and release the file to the OS }
    end;
    [/code]
    If you are not using TP, I still think you need to close the file connection, but you need to find which procedure to call from the language documentation.
  • mariblomariblo Posts: 4Member
    : : Can someone please help me to find out why the filehandling doesn't work? If you need to see the rest of the subroutines I will post them too. Thank you!!!
    : :
    : : [Code] PROGRAM inlupp6(Input, Output);
    : :
    : : CONST
    : : kundmax = 100;
    : : kreditmax = 5000;
    : :
    : : TYPE
    : : kundpektyp = ^transaktionstyp;
    : : string_30 = PACKED ARRAY[1..30] of Char;
    : : string_12 = PACKED ARRAY[1..12] of Char;
    : : kundtyp = RECORD
    : : namn : string_30;
    : : gadress : string_30;
    : : padress : string_30;
    : : tele : string_12;
    : : saldo : Integer;
    : : kredit : Integer;
    : : first : kundpektyp;
    : : END; {kundtyp}
    : : transaktionstyp = RECORD
    : : datum : string_12;
    : : transtyp : string_12;
    : : transaktion : Integer;
    : : next : kundpektyp;
    : : END; {transaktionstyp}
    : : regtyp = RECORD
    : : kundarr : ARRAY[1..kundmax] of kundtyp;
    : : antal : Integer;
    : : END; {regtyp}
    : : kundfil = FILE of kundtyp;
    : : transfil = FILE of transaktionstyp;
    : :
    : : VAR
    : : kom, svar : char;
    : : kundreg : regtyp;
    : :
    : : PROCEDURE loadtrans(VAR kunden : regtyp);
    : : VAR
    : : tfil : transfil;
    : : ny, sist : kundpektyp;
    : : i : Integer;
    : : BEGIN
    : : RESET(tfil, 'transaktion.dat');
    : : FOR i:=1 TO kunden.antal DO BEGIN
    : : IF kunden.kundarr[i].first<>NIL THEN BEGIN
    : : kunden.kundarr[i].first:=NIL;
    : : REPEAT
    : : New(ny);
    : : Read(tfil, ny^);
    : : IF kunden.kundarr[i].first=NIL THEN
    : : kunden.kundarr[i].first:=ny
    : : ELSE
    : : sist^.next:=ny;
    : : sist:=ny;
    : : UNTIL ny^.next=NIL;
    : : END; {IF}
    : : END; {FOR}
    : : END; {ladda}
    : :
    : : PROCEDURE loadkund(VAR kunden : regtyp);
    : : VAR
    : : kfil : kundfil;
    : : BEGIN
    : : RESET(kfil, 'kundreg.dat');
    : : kunden.antal:=0;
    : : WHILE NOT Eof(kfil) DO BEGIN
    : : kunden.antal:=kunden.antal+1;
    : : Read(kfil, kunden.kundarr[kunden.antal]);
    : : loadtrans(kunden);
    : : END; {WHILE}
    : : END; {loadkund}
    : :
    : : PROCEDURE savekund(VAR kunden : regtyp);
    : : VAR
    : : fil1 : kundfil;
    : : fil2 : transfil;
    : : temp : kundpektyp;
    : : i : Integer;
    : : BEGIN
    : : Rewrite(fil1, 'kundreg.dat');
    : : Rewrite(fil2,'transaktion.dat');
    : : FOR i:=1 TO kunden.antal DO BEGIN
    : : Write(fil1, kunden.kundarr[i]);
    : : temp:=kunden.kundarr[i].first;
    : : WHILE temp<>NIL DO BEGIN
    : : Write(fil2, temp^);
    : : temp:=temp^.next;
    : : END; {WHILE}
    : : END; {FOR}
    : : END; {savekund}
    : :
    : : BEGIN {HP}
    : : Writeln('Welcome! ');
    : : Writeln('Do you want to start? Y/N ');
    : : Readln(svar);
    : : svar:= versal(svar);
    : : IF (svar='Y') THEN
    : : loadkund(kundreg);
    : : REPEAT
    : : readkom(kom);
    : : CASE kom OF
    : : 'N' : donykund(kundreg);
    : : 'T' : dotrans(kundreg);
    : : 'S' : skriv_trans(kundreg);
    : : 'Q' : ;
    : : END; {CASE}
    : : UNTIL kom='Q';
    : : savekund(kundreg);
    : : END.
    : : [/CODE]
    : :
    : :
    : If you are using TP, you first neet to call assign() before calling the reset() and after you have read/written the data, you need to call close(). Example:
    : [code]
    : var
    : f: file of char;
    : begin
    : Assign(f, 'test.dat'); { Connect the file variable to a filename }
    : Rewrite(f); { Open the file for writing }
    : write(f, 'b', 'l', 'a');
    : Close(f); { Close the file connection, and release the file to the OS }
    : end;
    : [/code]
    : If you are not using TP, I still think you need to close the file connection, but you need to find which procedure to call from the language documentation.
    :
    No- I'm using GNU Pascal and it would be wrong to close the files.. I have no idea what the problem is but I hope I'll find it out today.
    Thank you anyway, it's great with such fast answers...

  • ManningManning Posts: 1,621Member
    : : If you are using TP, you first neet to call assign() before calling the reset() and after you have read/written the data, you need to call close(). Example:
    : : [code]
    : : var
    : : f: file of char;
    : : begin
    : : Assign(f, 'test.dat'); { Connect the file variable to a filename }
    : : Rewrite(f); { Open the file for writing }
    : : write(f, 'b', 'l', 'a');
    : : Close(f); { Close the file connection, and release the file to the OS }
    : : end;
    : : [/code]
    : : If you are not using TP, I still think you need to close the file connection, but you need to find which procedure to call from the language documentation.
    : :
    : No- I'm using GNU Pascal and it would be wrong to close the files.. I have no idea what the problem is but I hope I'll find it out today.
    : Thank you anyway, it's great with such fast answers...

    Why would it be wrong to close the files? The first thing I saw wrong was also what zibadian pointed out.
  • mariblomariblo Posts: 4Member
    : : : If you are using TP, you first neet to call assign() before calling the reset() and after you have read/written the data, you need to call close(). Example:
    : : : [code]
    : : : var
    : : : f: file of char;
    : : : begin
    : : : Assign(f, 'test.dat'); { Connect the file variable to a filename }
    : : : Rewrite(f); { Open the file for writing }
    : : : write(f, 'b', 'l', 'a');
    : : : Close(f); { Close the file connection, and release the file to the OS }
    : : : end;
    : : : [/code]
    : : : If you are not using TP, I still think you need to close the file connection, but you need to find which procedure to call from the language documentation.
    : : :
    : : No- I'm using GNU Pascal and it would be wrong to close the files.. I have no idea what the problem is but I hope I'll find it out today.
    : : Thank you anyway, it's great with such fast answers...
    :
    : Why would it be wrong to close the files? The first thing I saw wrong was also what zibadian pointed out.
    :
    In GNU P it's not allowed to close the files. I'll give you an example of how filehandling is done:
    [code]
    TYPE
    afile=FILE OF Integer;
    VAR
    f1:afile;
    fdat:Integer;

    BEGIN
    Rewrite(f1, 'filename.bla');
    fdat:=12345;
    Write(f1, fdat);
    END;
    END.
    [/CODE]

    That's it.

  • zibadianzibadian Posts: 6,349Member
    : : : : If you are using TP, you first neet to call assign() before calling the reset() and after you have read/written the data, you need to call close(). Example:
    : : : : [code]
    : : : : var
    : : : : f: file of char;
    : : : : begin
    : : : : Assign(f, 'test.dat'); { Connect the file variable to a filename }
    : : : : Rewrite(f); { Open the file for writing }
    : : : : write(f, 'b', 'l', 'a');
    : : : : Close(f); { Close the file connection, and release the file to the OS }
    : : : : end;
    : : : : [/code]
    : : : : If you are not using TP, I still think you need to close the file connection, but you need to find which procedure to call from the language documentation.
    : : : :
    : : : No- I'm using GNU Pascal and it would be wrong to close the files.. I have no idea what the problem is but I hope I'll find it out today.
    : : : Thank you anyway, it's great with such fast answers...
    : :
    : : Why would it be wrong to close the files? The first thing I saw wrong was also what zibadian pointed out.
    : :
    : In GNU P it's not allowed to close the files. I'll give you an example of how filehandling is done:
    : [code]
    : TYPE
    : afile=FILE OF Integer;
    : VAR
    : f1:afile;
    : fdat:Integer;
    :
    : BEGIN
    : Rewrite(f1, 'filename.bla');
    : fdat:=12345;
    : Write(f1, fdat);
    : END;
    : END.
    : [/CODE]
    :
    : That's it.
    :
    :
    I think I see what is wrong with your original code. In this code you have an end matching the rewrite(), but in the original code, there aren't any end's matching the reset()'s and rewrite()'s.
Sign In or Register to comment.