首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C语言 >

关于scanf(" %c"&select)与scanf("%c"&select)解决办法

2012-05-11 
关于scanf( %c,&select)与scanf(%c,&select)#includestdio.h#includestdlib.h#includeassert.h#

关于scanf(" %c",&select)与scanf("%c",&select)
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#include<string.h>
#define N 8

int main(void)
{
typedef struct
{
int num;
char name[18];
char sex[3];
int age;
}StudentType;
StudentType student;
StudentType aStudent[N]=
{
{200301,"张学友","男",43},
{200302,"刘德华","男",42},
{200303,"郭富城","男",45},
{200304,"黎天王","男",41},
{200305,"张靓颖","女",23},
{200306,"李宇春","女",22},
{200307,"窦文涛","男",43},
{200308,"孔夫子","男",36},
};
char fileName[80];
FILE *pFile;
int i;
char select='y';

printf("输入文件名:");
gets(fileName);
if((pFile=fopen(fileName,"wb+"))==NULL)
{
printf("打开文件%s失败!\n""敲任意键退出!\n",fileName);
system("pause");
exit(1);
}
fwrite(&aStudent,sizeof(StudentType),N,pFile);
while(select=='y')
{
rewind(pFile);
printf("输入要显示的学生序号(1~%d):",N);
scanf("%d",&i);
assert(i>=1&&i<=N);
fseek(pFile,sizeof(StudentType)*(i-1),SEEK_CUR);
fread(&student,sizeof(StudentType),1,pFile);
printf("\n学号:%d\n姓名:%s\n性别:%s\n年龄:%d\n\n",student.num,student.name,student.sex,student.age);

printf("是否继续(Y/N):");
scanf("%c",&select);
select=tolower(select);
assert(select=='y'||select=='n');
}
fclose(pFile);
system("pause");
return 0;
}

这里scanf(" %c",&select),如若没有空格即:scanf("%c",&select),程序将会由assert结束
执行结果:
  调用scanf()结束:将不会进行输入,直接进行后面的运算,导致assert,退出程序。
新手,呵呵,自己手动调试结果: 在调用scanf()前:&select 值:0x0012fe04 "y烫?" select 值: 121 'y'
  调用scanf()后:&select 值:0x0012ffe04 "烫?" select 值:10 ''

这里就糊涂了,scanf() 对这种情况为什么会有这种结果,scanf()处理这种情况的详细细节。
新人分不多,求高手解析,拜谢!!
   
 

[解决办法]
由于C一般没有把最后一个回车键处理掉,而是停留在缓冲区内。从而导致你scanf的时候获得到了回车键。参考下代码。

C/C++ code
#define N 8int main(void){    typedef struct    {        int num;        char name[18];        char sex[3];        int age;    }StudentType;    StudentType student;    StudentType aStudent[N]=    {        {200301,"张学友","男",43},        {200302,"刘德华","男",42},        {200303,"郭富城","男",45},        {200304,"黎天王","男",41},        {200305,"张靓颖","女",23},        {200306,"李宇春","女",22},        {200307,"窦文涛","男",43},        {200308,"孔夫子","男",36},    };    char fileName[80];    FILE *pFile;    int i;    char select='y';    printf("输入文件名:");    gets(fileName);    if((pFile=fopen(fileName,"wb+"))==NULL)    {        printf("打开文件%s失败!\n""敲任意键退出!\n",fileName);        system("pause");        exit(1);    }    fwrite(&aStudent,sizeof(StudentType),N,pFile);    while(select=='y')    {        rewind(pFile);        printf("输入要显示的学生序号(1~%d):",N);        scanf("%d",&i);        getchar();        assert(i>=1&&i<=N);        fseek(pFile,sizeof(StudentType)*(i-1),SEEK_CUR);        fread(&student,sizeof(StudentType),1,pFile);        printf("\n学号:%d\n姓名:%s\n性别:%s\n年龄:%d\n\n",student.num,student.name,student.sex,student.age);        printf("是否继续(Y/N):");        scanf("%c",&select);        select=tolower(select);        assert(select=='y'||select=='n');    }    fclose(pFile);    system("pause");    return 0;} 


[解决办法]
//下面三种方法任一种都行,都是处理输入缓冲区里的留下的回车字符
 getchar(); //吸收在输入缓冲区的上面输入的回车字符
// rewind(stdin); //位置指针重新指向一个流(数据流/文件)的开头

// fflush(stdin); //清空输入缓冲区



探讨
恩,加上之后可以了。能详细解释下么?

热点排行