boiko
Newbie | Редактировать | Профиль | Сообщение | Цитировать | Сообщить модератору Помогите пожалуйста! Написал программу,которая должна сохранять,добавлять и читать список из файла.Но возникли проблемы с чтением списка из файла и выводом списка на консоль. Заранее благодарен. #include<stdio.h> #include<stdlib.h> #define N 3 struct student { char name[50]; char surname[50]; unsigned char year; struct student* next; struct student* prev; } ; struct student* InputElement(){ struct student* p=(struct student*)malloc(sizeof(struct student)); printf("input name:\n"); scanf("%s",p->name); printf("input surname:\n"); scanf("%s",p->surname); printf("input age:\n"); scanf("%d",&(p->year)); p->next=NULL; p->prev=NULL; return p; }; struct student* AddToList(struct student* root,struct student* p){ if(NULL==p){ return root; }; p->next=root; p->prev=p->next; return p; }; void Max(struct student* root){ if(NULL==root) return; struct student* p=root; int max; while(p->next!=NULL){ if (p->next->year>max) { max=p->next->year; } p=p->next; } printf("A senior student is %d\n**********************\n",max); }; void Output(struct student* p){ while(p!=NULL){ printf("%s\t",p->name); printf("%s\t",p->surname); printf("%d\n",p->year); p=p->next; }; }; void FreeList(struct student* root){ if(NULL==root) return; struct student* q=root; struct student* p=root->next; struct student* f=root->prev; while (q){ if(q==NULL){ return; } free(q); q=p; if(q){ p=q->next; f=q->prev; }; }; }; long size(FILE*f){ long current=ftell(f); fseek(f,0,SEEK_END); long size=ftell(f); fseek(f,current,SEEK_SET); return size; } struct student* root=NULL; void write(){ for(int i=0;i<N;i++){ root=AddToList(root,InputElement()); }; struct student*p=root; FILE*f2=fopen("student.txt","w"); while(p!=NULL){ fwrite(p,sizeof(p),1,f2); p=p->next; }; Max(root); Output(root); FreeList(root); fclose(f2); } void read(){ FILE*f2=fopen("student.txt","r"); struct student*p=(struct student*)malloc(sizeof(struct student)); fread(&p,sizeof(p),1,f2); while(p!=NULL){ for(int i=0;i<N;i++){ p=AddToList(root,p); }; p=p->next; } Output(root); fclose(f2); } void appeand(){ FILE*f2=fopen("student.txt","a"); read(); long offset=ftell(f2); void*e=(FILE*)malloc(offset); for(int i=0;i<N;i++){ root=AddToList(root,InputElement()); } struct student*p=root; while(p!=NULL){ fwrite(e,sizeof(p),1,f2); p=p->next; }; free(e); Max(root); Output(root); FreeList(root); fclose(f2); } void main(){ int choice; printf("1-read\n2-write\n3-appeand\n"); scanf("%d",&choice); if(choice==1) read(); if(choice==2) write(); if(choice==3) appeand(); } P.S.Очень нужен код программы на Си ,которая создаёт бинарное дерево,записывает дерево в файл и выводит дерево на консоль из файла. P.P.S.Если сдам прогу с деревьями получу зачёт,так что HELP PLEASE!!! | Всего записей: 20 | Зарегистр. 27-11-2007 | Отправлено: 18:29 24-04-2008 | Исправлено: boiko, 18:37 24-04-2008 |
|