文件读入单词一直不停是为什么呢?调试后显示这个 program received signal sigsegv,segmentation fault
是个新手实在找不到为什么结果就是屏幕输出文件的内容后又继续无限输出空白新建的文件也是空白的#include<stdio.h>#include<stdlib.h>#defi...
是个新手 实在找不到为什么结果就是 屏幕输出文件的内容后又继续无限输出空白
新建的文件也是空白的
#include<stdio.h>
#include<stdlib.h>
#define LEN sizeof(struct Words)
struct Words
{char wd[20];
struct Words*next;
};
FILE *in,*out;
struct Words*creat(void)
{struct Words*head,*p1,*p2;
int n=0;
p1=p2=(struct Words*)malloc(LEN);
fscanf(in,"%s",p1->wd);
printf("%s\n",p1->wd);
head=NULL;
while(p1->wd!=0)//??
{n=n+1;
if(n==1)head=p1;
else p2->next=p1;
p2=p1;
p1=(struct Words*)malloc(LEN);
fscanf(in,"%s",p1->wd);
printf("%s\n",p1->wd);
}
p2->next=NULL;
return(head);
}
int main()
{struct Words*pt;
char infile[20];
printf("Enter file name:");
scanf("%s",infile);
in=fopen(infile,"r");
out=fopen("words2.txt","w");
if(in==NULL||out==NULL)
{printf("Open the file failure...\n");
exit(0);
}
printf("Opening file...");
pt=creat();
while(pt==NULL)
{fprintf(out,"%s\n",pt->wd);
printf("%s\n",pt->wd);
pt=pt->next;
}
fclose(in);
getchar();
return 0;
} 展开
新建的文件也是空白的
#include<stdio.h>
#include<stdlib.h>
#define LEN sizeof(struct Words)
struct Words
{char wd[20];
struct Words*next;
};
FILE *in,*out;
struct Words*creat(void)
{struct Words*head,*p1,*p2;
int n=0;
p1=p2=(struct Words*)malloc(LEN);
fscanf(in,"%s",p1->wd);
printf("%s\n",p1->wd);
head=NULL;
while(p1->wd!=0)//??
{n=n+1;
if(n==1)head=p1;
else p2->next=p1;
p2=p1;
p1=(struct Words*)malloc(LEN);
fscanf(in,"%s",p1->wd);
printf("%s\n",p1->wd);
}
p2->next=NULL;
return(head);
}
int main()
{struct Words*pt;
char infile[20];
printf("Enter file name:");
scanf("%s",infile);
in=fopen(infile,"r");
out=fopen("words2.txt","w");
if(in==NULL||out==NULL)
{printf("Open the file failure...\n");
exit(0);
}
printf("Opening file...");
pt=creat();
while(pt==NULL)
{fprintf(out,"%s\n",pt->wd);
printf("%s\n",pt->wd);
pt=pt->next;
}
fclose(in);
getchar();
return 0;
} 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询