Perche' mi da' segmentatio fault ???
codice:#include <malloc.h> #include <string.h> #include <stdio.h> struct __linefile { char *line; struct __linefile *next; }; struct __linefile *__create_linefile_list (FILE *fp) { struct __linefile *paus; /* Temp Pointer */ struct __linefile *p; /* List Pointer */ char buf[1024]; if (!feof(fp)) { if ((p = (struct __linefile *) malloc (sizeof(struct __linefile))) == NULL) { return (NULL); /* I Can't allocate memory for the List... */ } fgets(buf, (sizeof(buf)/sizeof(buf[0]))-1, fp); if ((p->line = (char *) malloc ((1 + strlen(buf))*sizeof(char))) == NULL) { return (NULL); /* I Can't allocate memory for the String... */ } strcpy (p->line, buf); paus = p; while (!feof(fp)) { if ((paus->next = (struct __linefile *) malloc (sizeof(struct __linefile))) == NULL) { return (NULL); /* I Can't allocate memory for the List... */ } paus = paus->next; fgets(buf, (sizeof(buf)/sizeof(buf[0]))-1, fp); if ((p->line = (char *) malloc ((1 + strlen(buf))*sizeof(char))) == NULL) { return (NULL); /* I Can't allocate memory for the String... */ } strcpy (p->line, buf); } paus->next = NULL; return (p); } return (NULL); /* Empty List... */ } int main() { struct __linefile *list; FILE *fp; fp = fopen("test.c", "rt"); list = __create_linefile_list(fp); if (list == NULL) printf ("Error"); fclose (fp); while (list != NULL) { puts (list->line); list = list->next; } return (0); }

Rispondi quotando