为什么这个c语言程序有时候运行是正确的,有时是错误的?
#include"stdio.h"#include"stdlib.h"#include"string.h"#defineLENsizeof(structshuju)str...
#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();
}
我试过好多次,有些时候程序能运行,有时程序运行不起来。真心不知道怎么回事啊,是不是系统有问题呢? 展开
#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();
}
我试过好多次,有些时候程序能运行,有时程序运行不起来。真心不知道怎么回事啊,是不是系统有问题呢? 展开
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询