Verkettete liste in datei ausgeben
-
Hallo,
ich habe folgendes Problem:
ich müsste eine verkettete Liste - die zuerst aufgefüllt wird - mit einer Unterfunktion in eine Datei ausgeben. Ich habe das Problem, das ich beim ausgeben nur komische Zeichen bekomme.
Hier ist das Program, es handelt sich um die unterfunktion WFILE[cpp]
#include <stdio.h>
#include <conio.h>
#define ESC 27
#include <io.h>
#include <sys\stat.h>
#include <fcntl.h>
#include <dos.h>
#define FN "c:\\1.txt"
#include <string.h>
# include <stdlib.h>struct TLista
{
struct TLista *kov;
struct TLista *elozo;
int age;
char name[31];
long loan;
}
*elso=NULL,*utolso=NULL;/struct TFile
{int age;
char name[31];
double loan;
}sv;/void OUT (void)
{
struct TLista *p=elso;
textbackground(GREEN);textcolor(BLACK);window(20,5,60,20);
clrscr();
gotoxy(20,2);
while(p!=NULL)
{
cprintf("\n%30s %8i %6li",p->name,p->age,p->loan);
p=p->kov;
}
getch();
}void KILLALL(void)
{
struct TLista *p=elso,*halott;
while(p!=NULL)
{ halott=p;
p=p->kov;
free(halott);
}
elso=NULL;utolso=NULL;
}void IN(void)
{
struct TLista p;
textbackground(GREEN);textcolor(BLACK);window(20,5,60,20);clrscr();
p=(struct TLista) malloc(sizeof(struct TLista));
gotoxy(1,1);cprintf("k‚rem a nevet: "); scanf("%31s",&p->name);
gotoxy(1,2);cprintf("k‚rem a kort: "); scanf("%i",&p->age);
gotoxy(1,3);cprintf("k‚rem a fizetest: ");scanf("%li",&p->loan);
if(elso==NULL)
{
utolso=p;
p->kov=NULL;
}
else
{
elso->elozo=p;
p->kov=elso;
}
elso=p;
elso->elozo=NULL;
}
void COUNT(void){}**void WFILE(void){
FILE *f;
struct TLista *p;
f= fopen( "c:\\1.txt", "wt" ) ;
if( f == NULL ){
fprintf( stderr, "\nError!\n" );
}
else{
fwrite(&elso,10,1,f);
fclose(f);}
}**
void RFILE(void){}
void DEL(void)
{remove(FN);}
void main(void)
{
int c;_setcursortype(_NOCURSOR);
do
{
textbackground(BLACK);clrscr();
textbackground(BLUE);window(22,6,62,21);clrscr();
textbackground(GREEN);textcolor(BLACK);window(20,5,60,20);
clrscr();
gotoxy(18,1);cprintf("MENU");
gotoxy(10,4);cprintf("1. :Bekeres");
gotoxy(10,5);cprintf("2. :List z s");
gotoxy(10,6);cprintf("3. :T”rl‚s");
gotoxy(10,7);cprintf("4. :Atlagot sz mol");
gotoxy(10,8);cprintf("5. :F jlba ¡rat s");
gotoxy(10,9);cprintf("6. :F jlb¢l bet”lt");
gotoxy(10,10);cprintf("7. :F jl t”rl‚se");
gotoxy(14,16);cprintf("ESC : Kilepes");
c=getch();
switch(c)
{
case '1':IN();break;
case '2':OUT();break;
case '3':KILLALL();break;
case '4':COUNT();break;
case '5':WFILE();break;
case '6':RFILE();break;
case '7':DEL();break;
}
}while(c!=ESC);_setcursortype(_NORMALCURSOR);
textbackground(BLACK);textcolor(LIGHTGRAY);clrscr();
}[/cpp]
danke im vor raus. Gruss Memphis
-
du musst jedes listenelement in einer schleife einzeln mit fprintf() ausgeben:
TList *p; for(p=first; p; p=p->next) fprintf(f,"age=%d\nloan=%ld\nname=%s\n\n",p->age,p->loan,p->name);
-
Danke
daran hatte ich noch nicht gedacht...