Pascal

Moderators: None (Apply to moderate this forum)
Number of threads: 4095
Number of posts: 14004

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
What's wrong? Posted by mariblo on 3 Dec 2002 at 11:30 AM
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!!!

 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.


Report
Re: What's wrong? Posted by zibadian on 3 Dec 2002 at 12:49 PM
: 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!!!
:
:
 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.
: 

:
:
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:
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;

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.
Report
Re: What's wrong? Posted by mariblo on 3 Dec 2002 at 10:01 PM
: : 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!!!
: :
: :
 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.
: : 

: :
: :
: 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:
:
: 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;
: 

: 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...

Report
Re: What's wrong? Posted by Manning on 4 Dec 2002 at 8:10 AM
: : 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:
: :
: : 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;
: : 

: : 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.
Report
Re: What's wrong? Posted by mariblo on 5 Dec 2002 at 1:30 AM
: : : 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:
: : :
: : : 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;
: : : 

: : : 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:
TYPE
afile=FILE OF Integer;
VAR
f1:afile;
fdat:Integer;

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


That's it.

Report
Re: What's wrong? Posted by zibadian on 5 Dec 2002 at 3:50 AM
: : : : 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:
: : : :
: : : : 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;
: : : : 

: : : : 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:
:
: TYPE
: afile=FILE OF Integer;
: VAR
: f1:afile;
: fdat:Integer;
: 
: BEGIN
: Rewrite(f1, 'filename.bla');
: fdat:=12345;
: Write(f1, fdat);
: END;
: END.
: 

:
: 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.



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.