为什么这个c语言程序有时候运行是正确的,有时是错误的?

#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#define LEN sizeof(struct shuju)
struct shuju
{
char name[20];
char writer[20];
int no;
char press[20];
char date[8];
float price;
struct shuju *next;
};
void duqu()
{
FILE *fp;struct shuju *head2,*tail,*new1;int count=0,i;
fp=fopen("10000.txt","r");
for(i=1;i<=2;i++)
{
new1=(struct shuju *)malloc(LEN);
fscanf(fp,"%s",new1->name);
fscanf(fp,"%s",new1->writer);
fscanf(fp,"%d",&new1->no);
fscanf(fp,"%s",new1->press);
fscanf(fp,"%s",new1->date);
fscanf(fp,"%f",&new1->price);
count++;
new1->next=NULL;
if(count==1)
{
head2=new1;
}
else
tail->next=new1;
tail=new1;
}
printf("1");
fclose(fp);
}
void main()
{
duqu();
}
我试过好多次,有些时候程序能运行,有时程序运行不起来。真心不知道怎么回事啊,是不是系统有问题呢?

第1个回答  2011-05-24
一般fopen时要判断下:
if((fp = fopen("1000.txt","r")) == NULL)
{
printf("error open file!\n");
exit(0);
}
好像没什么问题了。
再就是楼主编程风格太烂了。建议上网找下《高质量c++、c 编程指南》看看。追问

怎么个烂法啊?你帮我代码修改下我学习下。谢谢了。

追答

网上下个《高质量c++、c 编程指南》看看

相似回答