#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,n,no;char name[20],a[20]="000",b[20]="000",writer[20],press[20];
fp=fopen("10000.txt","r");
for(i=1;iname);
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;
}
fclose(fp);
if(n==1)
{
printf("qing shu ru shu ming:");
scanf("%s",name);
do
{
if(strcmp(head2->name,name)==0)
{
printf("%s%s",head2->name,head2->writer);
strcpy(a,head2->name);
}
else
head2=head2->next;
}while(head2==NULL);
if(a=="000")
printf("not founf!\n");
strcpy(a,b);
}
}
void main()
{
duqu();
}
10000.txt里面内容是:bashitian zhangsan 10 beijing 19920223 20.000000 cyuyan lisi 11 nanjing 19930331 31.000000
我有一次试是成功的,能完成所想的东西,再加后面的东西就出错了。不知道怎么回事。只要这个程序能运行就可以了,不用做其他地方修改。
修改完的程序:(字数太多了,删了些)
if((fp=fopen("10000.txt","r"))==NULL)
printf("cannot open this file!\n");
for(i=1;iname);
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=tail=new1;
}
else
tail->next=new1;
tail=new1;
}
fclose(fp);
printf("qing shu ru yao cha xun de lei bie:");
printf("1.shuming\n2.zuozhe\n3.shuhao\n4.chubanshe\n");
scanf("%d",&n);
if(n==1)
{
printf("qing shu ru shu ming:");
scanf("%s",name);
do
{
if(strcmp(head2->name,name)==0)
{
printf("%s %s",head2->name,head2->writer);
strcpy(a,head2->name);
}
else
head2=head2->next;
}while(head2!=NULL);
if(strcmp(a,"000")==0)
printf("not founf!\n");
strcpy(a,b);
}
|