Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
11.3k views
in Technique[技术] by (71.8m points)

list - How to save my structure into a file in C?

I wrote this structure that allows me to add an entry with a person's name, their zip-code and their telephone number. In my Menu Option 5, I want to save the added entries into a file. I have tried looking at tutorials, but it doesn't seem to work for me.

 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 struct parts{
 char surname[30];
 char forename[30];
 int area_code;
 int tel_nr;
 struct parts *next;
 };
 typedef struct parts *tparts; // neuer Datentyp: Zeiger auf parts

 void printlist(tparts elp){ // display list
 printf("
***********************************
");
 while(elp!=NULL) {
 printf("Nachname: %s
",elp->surname);
 printf("Vorname: %s 
",elp->forename);
 printf("Vorwahlnummer: %d
",elp->area_code);
 printf("Telefonnummer: %d
",elp->tel_nr);
 elp=elp->next;
 }
 printf("***********************************
");return;
 }

 void printlisttofile(tparts elp){ // print list into File
 printf("
***********************************
");
 while(elp!=NULL) {
 fprintf("Nachname: %s
",elp->surname);
 fprintf("Vorname: %s 
",elp->forename);
 fprintf("Vorwahlnummer: %d
",elp->area_code);
 fprintf("Telefonnummer: %d
",elp->tel_nr);
 elp=elp->next;
 }
 printf("***********************************
");return;
 }

 tparts allocate(void){ // Speicher f¨ur Listenelemente bereit stellen
 tparts elp;
 elp=(tparts)malloc(sizeof(*elp));
 if(elp==NULL){printf("Error malloc"); exit(1);}
 elp->next=NULL;
 return elp;
 }

 // Listenelement einf¨ugen, sodass Liste aufsteigend nach Artikelnummer sortiert ist
 void el_insert(tparts partslist, tparts q){
 tparts p=partslist;
 while(p->next!=NULL && strcmp(p->next->surname,q->surname) < 0 ) p=p->next; // EInfügeposition
 q->next=p->next;
  p->next=q;
 return;
 }

 void el_delete(tparts partslist,int tel_nr){ // Element mit gegebener Artikelnummer l¨oschen
 tparts p=partslist,q;
 while(p->next!=NULL && p->next->tel_nr<tel_nr) p=p->next;// zu l¨oschendes Element finden
 if(p->next!=NULL && p->next->tel_nr==tel_nr){
 q=p->next; p->next=p->next->next;
 free(q);
 printf("Die gewünschten Eintr?ge wurden gel?scht");
 }
 return;
 }
void el_search(tparts partslist,char nachname,char vorname) {
tparts p=partslist,q;
while(p->next!=NULL) {

        if(p->surname == nachname && p->forename == vorname) {
          
        }
            p=p->next;
}
return;
}

 int main(){
 FILE *fp;
 unsigned int op;
 int tel_nr; 
 char vorname;
 char nachname;
 tparts partslist,
 elp; 
 partslist=allocate(); 
 do{ 
 printf("
 1) Telefonnummer suchen " );
 printf("
 2) Telefonverzeichnis anzeigen " );
 printf("
 3) Neuer eintrag " );
 printf("
 4) Telefonnummer l?schen " );
 printf("
 5) Verzeichnis in Datei speichern" );
 printf("
 6) STOP 
" );
 scanf("%u",&op);
 switch(op){ 
 case 1:
    printf("
 Nachname der gesuchten Person: "); scanf("%s",&nachname);
    printf("
 Vorname der gescuhten Person: "); scanf("%s",&vorname);
    el_search(partslist,nachname,vorname);
     break;

 case 2: printlist(partslist->next);
    break;

 case 3:elp=allocate();
    printf("
 Nachname: "); scanf("%s",&(elp->surname));
    printf("
 Vorname: "); scanf("%s",&(elp->forename));
    printf("
 Vorwahl: "); scanf("%d",&(elp->area_code));
    printf("
 Telefonnummer: "); scanf("%d",&(elp->tel_nr));
    el_insert(partslist,elp);
    break;
 case 4:
    printf("
 Zu l?schende Telefonnummer = ");
    scanf("%d",&tel_nr);
    el_delete(partslist,tel_nr);
    break;
case 5:
        fp=fopen("verzeichnis.txt","wb");
        if(fp==NULL) {
            printf("
Error cannot open file");
            exit(1);
        }
        while(elp!=NULL) {
           
            fprintf("verzeichnis.txt","Nachname: %s
",elp->surname);
            fprintf("verzeichnis.txt","Vorname: %s
",elp->forename);
            fprintf("verzeichnis.txt","Vorwahl: %sd",elp->area_code);
            fprintf("verzeichnis.txt","Telefonnummer: %d
",elp->tel_nr);
            elp=elp->next;
        }
        fclose(fp);
        break;
 }
 }while(op!=6);
 return 0;
 }

The file "verzeichnis.txt" gets created, but it's always empty. Does someone have an idea?

question from:https://stackoverflow.com/questions/65878815/how-to-save-my-structure-into-a-file-in-c

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...