在把文件读进链表的时候出了问题,请问改怎么解决?帮忙修改一下
程序代码:
#include <stdio.h> #include <string.h> #include <stdlib.h> struct node { int data; char name[100]; char valu[100]; struct node *next; }; struct node * creat ( int n , char *argv ) //创建链表 { struct node *head, *p, *q; int i=0 ,t; char *pp , *qq , str[100]; head = (struct node *)malloc(sizeof(struct node)); // p = head; FILE *fp; fp = fopen( argv , "r"); /*while( fscanf(fp , "%d" , &t) != EOF ) { if( (q = ( struct node * )malloc(sizeof(struct node))) == NULL ) { printf("error!"); } else { q->data = t; //strcpy( q->name , "hello" ); p->next = q; p = q; } }*/ while( !feof(fp) ) { fgets(str , 100 , fp ); pp = strchr( str , '=' ); qq = pp + 1; *pp = '\0'; pp = NULL; if(pp = strchr( qq , '#' )) { *pp = '\0'; } if( (q = ( struct node * )malloc(sizeof(struct node))) == NULL ) { printf("error!"); } else { strcpy( q->name , delblank(str) ); strcpy( q->valu , delblank(qq) ); p->next = q; p = q; } } p->next = NULL; head = head->next; return(head); fclose(fp); } void output(struct node *head) //输出链表 { struct node *p; // p = head->next; p = head; while( p ) { printf("%s=%s\n" , p->name , p->valu); p=p->next; } } char * delblank( char *str ) { char *p ,*q; int len = 0; len = strlen(str); p = str; if( *str == ' ') { while( *p == ' ' ) { p++; } } q = str + len - 1; while( *q == ' ' ) { q--; } *(q + 1) = '\0'; return p; } int main( int argc , char * argv[] ) //主函数 { int number=5 , i=0; struct node *head, *searchp, *searchp2, *q; head = creat(number , argv[1] ); printf("原链表:\n"); output(head); printf("\n\n"); }
报错在图片里