带冲洗的输入流C题 [英] problem with flushing input stream C
问题描述
我不能够以标准输入这里刷新,有没有办法来刷新标准输入吗?如果没有,那么如何使的getchar()取一个字符作为用户输入,而不是\\ N由scanf函数在左输入缓冲区??
的#includestdio.h中
#包括stdlib.h中INT主(INT ARGC,CHAR *的argv []){
FILE * FP;
另一种烧焦='Y';
结构EMP {
焦炭名[40];
INT年龄;
浮BS;
};
结构EMPê;
如果(的argc!= 2){
的printf(请写出1目标文件名\\ n);
}
FP = FOPEN(的argv [1],WB);
如果(FP == NULL){
看跌期权(不能打开文件);
出口(1);
}
而(另=='Y'){
的printf(\\ n输入姓名,年龄和基本工资);
scanf函数(%s%D%F,e.name,与e.age,与e.bs);
FWRITE(&E的sizeof(E),1,FP); 的printf(添加其他记录(Y / N));
fflush(标准输入);
另一=的getchar();
}
FCLOSE(FP);
返回0;
}
编辑: - 更新code,仍不能正常工作
的#includestdio.h中
#包括stdlib.h中INT主(INT ARGC,CHAR *的argv []){
FILE * FP;
另一种烧焦='Y';
结构EMP {
焦炭名[40];
INT年龄;
浮BS;
};
结构EMPê;
无符号整型常量BUF_SIZE = 1024;
烧焦的buf [BUF_SIZE] 如果(的argc!= 2){
的printf(请写出1目标文件名\\ n);
}
FP = FOPEN(的argv [1],WB);
如果(FP == NULL){
看跌期权(不能打开文件);
出口(1);
}
而(另=='Y'){
的printf(\\ n输入姓名,年龄和底薪:);
与fgets(BUF,BUF_SIZE,标准输入);
sscanf的(BUF,%s%D%F,e.name,与e.age,与e.bs);
FWRITE(&E的sizeof(E),1,FP);
的printf(添加其他记录(Y / N));
另一=的getchar();
}
FCLOSE(FP);
返回0;
}这个输出是: - 开发@ DEV-笔记本:〜/文档/ C ++ _ PRAC / google_int_prac $ ./a.out emp.dat输入姓名,年龄和基本工资:deovrat 45 23
添加另一条记录(Y / N)Y输入姓名,年龄和基本工资:添加另一条记录(Y / N)Y输入姓名,年龄和基本工资:添加另一条记录(Y / N)
更新:您需要添加其他的getchar()在你的循环消耗的'\\ n'后面的Y / N的结束。我不认为这是最好的一段路要走,但它会使你的code的工作,因为它现在站立。
同时(另一个=='Y'){
的printf(\\ n输入姓名,年龄和底薪:);
与fgets(BUF,BUF_SIZE,标准输入);
sscanf的(BUF,%s%D%F,e.name,&安培; e.age,&安培; e.bs);
FWRITE(急症室,的sizeof(E),1,FP);
的printf(添加其他记录(Y / N));
另一=的getchar();
的getchar();
}
我建议读要分析(直到并包括所述的'\\ n')到一个缓冲区,然后使用sscanf的解析出来()中的数据。这样,你消耗的换行,您可以对数据执行其他的健康检查。
I am not able to flush stdin here,is there a way to flush stdin?If not then how to make getchar() to take a character as input from user, instead of a "\n" left by scanf in the input buffer??
#include "stdio.h" #include "stdlib.h" int main(int argc,char*argv[]) { FILE *fp; char another='y'; struct emp { char name[40]; int age; float bs; }; struct emp e; if(argc!=2) { printf("please write 1 target file name\n"); } fp=fopen(argv[1],"wb"); if(fp==NULL) { puts("cannot open file"); exit(1); } while(another=='y') { printf("\nEnter name,age and basic salary"); scanf("%s %d %f",e.name,&e.age,&e.bs); fwrite(&e,sizeof(e),1,fp); printf("Add another record (Y/N)"); fflush(stdin); another=getchar(); } fclose(fp); return 0; }
EDIT :- updated code ,still not working properly
#include "stdio.h" #include "stdlib.h" int main(int argc,char*argv[]) { FILE *fp; char another='y'; struct emp { char name[40]; int age; float bs; }; struct emp e; unsigned int const BUF_SIZE = 1024; char buf[BUF_SIZE]; if(argc!=2) { printf("please write 1 target file name\n"); } fp=fopen(argv[1],"wb"); if(fp==NULL) { puts("cannot open file"); exit(1); } while(another=='y') { printf("\nEnter name,age and basic salary : "); fgets(buf, BUF_SIZE, stdin); sscanf(buf, "%s %d %f", e.name, &e.age, &e.bs); fwrite(&e,sizeof(e),1,fp); printf("Add another record (Y/N)"); another=getchar(); } fclose(fp); return 0; } output for this is :- dev@dev-laptop:~/Documents/c++_prac/google_int_prac$ ./a.out emp.dat Enter name,age and basic salary : deovrat 45 23 Add another record (Y/N)y Enter name,age and basic salary : Add another record (Y/N)y Enter name,age and basic salary : Add another record (Y/N)
Update: You need to add another getchar() at the end of your loop to consume the '\n' that follows the Y/N. I don't think this is the best way to go, but it will make your code work as it stands now.
while(another=='y') {
printf("\nEnter name,age and basic salary : ");
fgets(buf, BUF_SIZE, stdin);
sscanf(buf, "%s %d %f", e.name, &e.age, &e.bs);
fwrite(&e,sizeof(e),1,fp);
printf("Add another record (Y/N)");
another=getchar();
getchar();
}
I would suggest reading the data you want to parse (up to and including the '\n') into a buffer and then parse it out using sscanf(). This way you consume the newline and you can perform other sanity checks on the data.
这篇关于带冲洗的输入流C题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!