求教,创建列表时为什么只读一行字符?真的真的很急

CodeMaker_12138 2011-12-03 06:54:41
WORDNODE* Create()//构建链表
{
int len=sizeof(WORDNODE),j,n,m;
char gwords[M+N]={0};
WORDNODE *p1,*p2,*h;
FILE *fp;
do
{
fp=fopen("dict.TXT","r");
}while(fp==NULL);
p1=p2=h=(WORDNODE*)malloc(len);
while(fgets(gwords,N+M,fp)!=NULL);
{printf("%s",gwords);
j=0;
m=0;
p1=p2;
while(!isspace(gwords[j]))
{
p1->Eword[j]=gwords[j];
j=j+1;
}
p1->Eword[j]='\0';//向Eword赋值
for(n=j+1;n<(int)strlen(gwords);n++)
{
p1->Chinese[m]=gwords[n];
m=m+1;
}
p1->Chinese[m]='\0';//向Chinese赋值
p2=(WORDNODE*)malloc(len);
p1->next=p2;//申请内存并把地址赋给next
}
p1->next=NULL;//next指空
free(p2);//这句有意义么?有什么意义?去掉的话会有影响吗?
fclose(fp);
return h;
}
...全文
137 12 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
CodeMaker_12138 2011-12-03
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 bhdgx 的回复:]

引用 10 楼 holly_worm 的回复:

之前出现过这样
printf(“value=%d”,value);
if(value==1);
{
printf(“word”);
}
即使打印出value=0也打印word,然后改成
if(value==1)
{
printf(“word”);
}else{;}就好了,可不可能是这样的事呢?拜托看一眼、、、真的很急很快就……
[/Quote]唔唔、、、多谢啦、非常非常感谢、
Soulic 2011-12-03
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 holly_worm 的回复:]

之前出现过这样
printf(“value=%d”,value);
if(value==1);
{
printf(“word”);
}
即使打印出value=0也打印word,然后改成
if(value==1)
{
printf(“word”);
}else{;}就好了,可不可能是这样的事呢?拜托看一眼、、、真的很急很快就得交、
[/Quote]

是这样的,

while(fgets(gwords,N+M,fp)!=NULL); ==> while(fgets(gwords,N+M,fp)!=NULL)

就好了
CodeMaker_12138 2011-12-03
  • 打赏
  • 举报
回复
之前出现过这样
printf(“value=%d”,value);
if(value==1);
{
printf(“word”);
}
即使打印出value=0也打印word,然后改成
if(value==1)
{
printf(“word”);
}else{;}就好了,可不可能是这样的事呢?拜托看一眼、、、真的很急很快就得交、
CodeMaker_12138 2011-12-03
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 bhdgx 的回复:]

先写个简单的读文件的代码,下面是从你代码截取的,测测能不能把文件内容全部读出来
C/C++ code
WORDNODE* Create()//构建链表
{
char gwords[M+N]={0};

FILE *fp;
do
{
fp=fopen("dict.TXT","r");
}while(fp==NULL);

whil……
[/Quote]不读啊?怎么这样嘞、、、
Soulic 2011-12-03
  • 打赏
  • 举报
回复
先写个简单的读文件的代码,下面是从你代码截取的,测测能不能把文件内容全部读出来
WORDNODE* Create()//构建链表
{
char gwords[M+N]={0};

FILE *fp;
do
{
fp=fopen("dict.TXT","r");
}while(fp==NULL);

while(fgets(gwords,N+M,fp)!=NULL);
{
printf("%s",gwords);
}

fclose(fp);
return NULL;
}
CodeMaker_12138 2011-12-03
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 bhdgx 的回复:]

你的dict.txt的内容是什么样的
[/Quote]
English 英语
,,, ,,
,,, ,,
Soulic 2011-12-03
  • 打赏
  • 举报
回复
你的dict.txt的内容是什么样的
CodeMaker_12138 2011-12-03
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 bhdgx 的回复:]

能从Create 退出吗
[/Quote]能,后面的打印,输入什么的都正常,但是只读一次就退出循环了
Soulic 2011-12-03
  • 打赏
  • 举报
回复
能从Create 退出吗
CodeMaker_12138 2011-12-03
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 zichen0422 的回复:]
free(),肯定有意义啊,你前面malloc了啊!所以要free掉啊!
[/Quote]然后嘞,请问为什么只能读进去一行字符串?
zichen0422 2011-12-03
  • 打赏
  • 举报
回复
free(),肯定有意义啊,你前面malloc了啊!所以要free掉啊!
CodeMaker_12138 2011-12-03
  • 打赏
  • 举报
回复
请帮忙看看、急死急死啦、、、

70,024

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧