blu1988
21-07-2010, 14:45
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define MAXLEN 10+2
#define MAX 255
typedef struct elenco_s elenco_t;
struct elenco_s{
char stato;
char sesso;
int id;
int day;
int h;
elenco_t *next;
};
elenco_t*load(FILE*);
elenco_t*NewE();
void check(elenco_t*);
main(){
FILE*fp;
elenco_t *elenco;
char nomef[MAX];
printf("Inserire nome file con soci>");
scanf("%s",nomef);
//open&check
fp=fopen(nomef,"r");
if(fp==NULL){
printf("errore apertura file");
exit(1);
}
elenco=load(fp);
fclose(fp);
check(elenco);
}
//funzioni
//newE
elenco_t*NewE(){
elenco_t*tmp;
tmp=(elenco_t*)malloc(1*sizeof(elenco_t));
if(tmp==NULL){
printf("errore allocazione");
exit(1);
}
return(tmp);
}
//load
elenco_t*load(FILE* fp){
char buff[MAXLEN];
char stato,sesso;
int id,day,h;
elenco_t*p;
elenco_t*head=NULL;
while(fgets(buff,MAXLEN,fp)!=NULL){
sscanf(buff,"%c:%c%d:%d:%d",&stato,&sesso,&id,&day,&h);
p=NewE();
p->stato=stato;
p->sesso=sesso;
p->id=id;
p->day=day;
p->h=h;
p->next=head;
head=p;
}
return(head);
}
//check
void check(elenco_t*elenco){
elenco_t*p;
p=elenco;
while(p!=NULL){
printf("stato=%c\nsesso=%c\nid=%d\nday=%d\nh=%d\n,p->stato,p->sesso,p->id,p->day,p->h");
p=p->next;}
}
ciao a tutti...vorrei sapere perchè quando inserisco il nome del file che deve leggermi e caricarmi in lista mi da errore apertura file(che sarebbe il comando che mi stampa quando fp==NULL)...dove sbaglio?
ps
es file inserito
E:M123:1:7\n
U:M123:1:7\n
#include<stdlib.h>
#include<string.h>
#define MAXLEN 10+2
#define MAX 255
typedef struct elenco_s elenco_t;
struct elenco_s{
char stato;
char sesso;
int id;
int day;
int h;
elenco_t *next;
};
elenco_t*load(FILE*);
elenco_t*NewE();
void check(elenco_t*);
main(){
FILE*fp;
elenco_t *elenco;
char nomef[MAX];
printf("Inserire nome file con soci>");
scanf("%s",nomef);
//open&check
fp=fopen(nomef,"r");
if(fp==NULL){
printf("errore apertura file");
exit(1);
}
elenco=load(fp);
fclose(fp);
check(elenco);
}
//funzioni
//newE
elenco_t*NewE(){
elenco_t*tmp;
tmp=(elenco_t*)malloc(1*sizeof(elenco_t));
if(tmp==NULL){
printf("errore allocazione");
exit(1);
}
return(tmp);
}
//load
elenco_t*load(FILE* fp){
char buff[MAXLEN];
char stato,sesso;
int id,day,h;
elenco_t*p;
elenco_t*head=NULL;
while(fgets(buff,MAXLEN,fp)!=NULL){
sscanf(buff,"%c:%c%d:%d:%d",&stato,&sesso,&id,&day,&h);
p=NewE();
p->stato=stato;
p->sesso=sesso;
p->id=id;
p->day=day;
p->h=h;
p->next=head;
head=p;
}
return(head);
}
//check
void check(elenco_t*elenco){
elenco_t*p;
p=elenco;
while(p!=NULL){
printf("stato=%c\nsesso=%c\nid=%d\nday=%d\nh=%d\n,p->stato,p->sesso,p->id,p->day,p->h");
p=p->next;}
}
ciao a tutti...vorrei sapere perchè quando inserisco il nome del file che deve leggermi e caricarmi in lista mi da errore apertura file(che sarebbe il comando che mi stampa quando fp==NULL)...dove sbaglio?
ps
es file inserito
E:M123:1:7\n
U:M123:1:7\n