当前位置: 代码迷 >> C语言 >> [求助]关于scanf不能正常赋值
  详细解决方案

[求助]关于scanf不能正常赋值

热度:296   发布时间:2006-10-16 21:49:11.0
[求助]关于scanf不能正常赋值

我为编写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;
}

哪位高手帮忙看一下啊?!

搜索更多相关的解决方案: 赋值  scanf  void  struct  int  

----------------解决方案--------------------------------------------------------
以下是引用兰风清寒在2006-10-16 21:49:11的发言:

我为编写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->a接收.
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;
}

哪位高手帮忙看一下啊?!


----------------解决方案--------------------------------------------------------
主要是字符留在了缓冲区里了,所以不能正常输入了
用语句fflush(stdin);来清除缓冲区里面的字符就能正常输入了
----------------解决方案--------------------------------------------------------

还别说,fflush(stdin);早就用了,没用啊!!我还曾经传给同学,在他的电脑上同样不能运行!
还有,t改成char也没用;

但是以下的程序又能正常运行:
#include<stdio.h>

void main (void)
{
char t;

printf("Is this a new set file?(Y/N)");
printf("%c",t);
scanf("%c",&t);
printf("Hello world 10");

printf("%c",t);
if (t=='y'||t=='Y')
{
printf("Hello world");
}
}


----------------解决方案--------------------------------------------------------
你试试将浮点运算打开,随便定义一个float a;a++;
----------------解决方案--------------------------------------------------------

可能是使用文件的原因.


----------------解决方案--------------------------------------------------------
scanf("%c%d%l",&p->a,&p->b,&p->c);

这个接收最好不要写在一行


----------------解决方案--------------------------------------------------------

我将原程序改为下面形式,可以正常运行,
先在这里谢谢各位大侠!!
――看来是程序读取数据出错!

――那么文件该怎么读取啊?特别对于十字链表!
――我的NULL是不是用多了?或者根本就不能这样用作判断的条件?

请各位大侠再次拔刀相助!
为我排解疑惑,在下感激不胜!
#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)
{
char t;
struct h *p,*head;
int flag=1,n;
FILE *fp;

printf("Is this a new set file?(Y/N)");
scanf("%*c%c",&t);
/*scanf("%c",&t);*/


printf("Hello world\n");

/*建立链表*/
while(flag){
p=(struct h *)malloc(sizeof(struct h));
printf("input:\t char a\tint b\tlong c\n");
scanf("%*c%c%d%ld",&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);
}


----------------解决方案--------------------------------------------------------
以下是引用兰风清寒在2006-10-17 15:35:48的发言:

我将原程序改为下面形式,可以正常运行,
先在这里谢谢各位大侠!!
――看来是程序读取数据出错!

――那么文件该怎么读取啊?特别对于十字链表!
――我的NULL是不是用多了?或者根本就不能这样用作判断的条件?

请各位大侠再次拔刀相助!
为我排解疑惑,在下感激不胜!
#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)
{
char t;
struct h *p,*head;
int flag=1,n;
FILE *fp;

printf("Is this a new set file?(Y/N)");
scanf("%*c%c",&t); //这句什么意思能说下吗
/*scanf("%c",&t);*/


printf("Hello world\n");

/*建立链表*/
while(flag){
p=(struct h *)malloc(sizeof(struct h));
printf("input:\t char a\tint b\tlong c\n");
scanf("%*c%c%d%ld",&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);
}


----------------解决方案--------------------------------------------------------
scanf("%*c%c",&t); //这句什么意思能说下吗
应该是跳过1字符再接收字符.
----------------解决方案--------------------------------------------------------

  相关解决方案