我为编写c语言的作业的数据保存,编写了一个小测试程序,但这个程序遇到问题,scanf不能正常赋值,当将scanf("%c",&t);改为t=getch();后这儿过去了,后面整型和float型又过不去,最主要的,不知道为什么会这样!!
#include<stdio.h>
#include<stdlib.h>
void write(FILE *fp);
void read(FILE *fp);
struct h{
char a;
int b;
long c;
struct h *next;
};
struct h *head;
void main (void)
{
int t,n;
struct h *p,*head;
int flag=1;
FILE *fp;
printf("Is this a new set file?(Y/N)");
scanf("%c",&t);
printf("Hello world 10");
if (t=='y'&&t=='Y')
{
printf("Hello world");
if((fp=fopen("mf","r"))==NULL) {
printf("Can't open file mf");
exit(-1);
}
printf("Hello world 1");
read(fp);
printf("Hello world 2");
}
else
{ printf("Hello world5");
if((fp=fopen("mf","r"))==NULL) {
printf("Can't open file mf");
exit(-1);
}
}
printf("Hello world");
/*建立链表*/
while(flag){
p=(struct h *)malloc(sizeof(struct h));
printf("input:\t char a\tint b\tlong c\n");
scanf("%c%d%l",&p->a,&p->b,&p->c);
p->next=NULL;
printf("input 0 to stop inputing!");
scanf("%d",&flag);
} /*end of while*/
/*保存数据*/
write(fp);
}
void write(FILE *fp)
{
struct h *p;
for(p=head;p!=NULL;p=p->next)
fwrite(p,sizeof(struct h),1,fp);
}
void read(FILE *fp)
{
struct h *p,*tail,buf;
fread(&buf,sizeof(struct h),1,fp);
p=(struct h *)malloc(sizeof(struct h));
*p=buf;head=p;
while(p!=NULL){
fread(&buf,sizeof(struct h),1,fp);
p=(struct h *)malloc(sizeof(struct h));
tail=p;*p=buf;
tail->next=p;
}
tail->next=p;
}
哪位高手帮忙看一下啊?!