11 04 结构体数组
C语言中的输入输出流和缓冲区(重点)详解
解释:为什么scanf被跳过或不执行
缓冲区
fflush(stdout);//过期
rewind(stdin);//清空缓存区
getchar();//本文件用来吃掉\n
printf("%d\n", BUFSIZ);//缓冲区大小,512B
好多人都会遇到这问题,因为scanf没读完所有缓存中的信息。把回车剩外面了,被下次的scanf接收到了。
缓冲区就像菜鸟驿站,协调用户(零零散散来拿)和快递员(一次处理很多件),一个慢,一个快的问题。
分为全、行、不带缓冲区,scanf遇到的问题属于行缓冲区
01 问题:for循环 输入次数和scanf对应不上
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct students
{
char name[21];
unsigned int age;
char tel[16];
float scores[3];
char sex;
};
void main()
{
struct students stu[2];
for (int i = 0; i < 2; i++) {
printf("姓名,年龄,电话,语文成绩,数学成绩,英语成绩,性别\n");
scanf("%s", stu[i].name);
scanf("%d", &stu[i].age);
scanf("%s", stu[i].tel);
scanf("%f", &stu[i].scores[0]);
scanf("%f", &stu[i].scores[1]);
scanf("%f", &stu[i].scores[2]);
scanf("%c", &stu[i].sex);
}
for (int i = 0; i < 2; i++) {
printf("%s\n", stu[i].name);
printf("%d\n", stu[i].age);
printf("%s\n", stu[i].tel);
printf("%.1f\n", stu[i].scores[0]);
printf("%.1f\n", stu[i].scores[1]);
printf("%.1f\n", stu[i].scores[2]);
printf("%s\n", stu[i].sex == 'M' ? "男" : "女");
}
system("pause");
}
01 01 原因:%c会直接读取缓冲区上一个字符
01 02 方法一:getchar()吃掉\n
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct students
{
char name[21];
unsigned int age;
char tel[16];
float scores[3];
char sex;
};
void main()
{
struct students stu[2];
for (int i = 0; i < 2; i++) {
printf("姓名,年龄,电话,语文成绩,数学成绩,英语成绩,性别\n");
scanf("%s", stu[i].name);
scanf("%d", &stu[i].age);
scanf("%s", stu[i].tel);
scanf("%f", &stu[i].scores[0]);
scanf("%f", &stu[i].scores[1]);
scanf("%f", &stu[i].scores[2]);
scanf("%c", &stu[i].sex);
getchar();
}
for (int i = 0; i < 2; i++) {
printf("%s\n", stu[i].name);
printf("%d\n", stu[i].age);
printf("%s\n", stu[i].tel);
printf("%.1f\n", stu[i].scores[0]);
printf("%.1f\n", stu[i].scores[1]);
printf("%.1f\n", stu[i].scores[2]);
printf("%s\n", stu[i].sex == 'M' ? "男" : "女");
}
system("pause");
}
01 03 方法二:rewind(stdin)清空缓存区
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct students
{
char name[21];
unsigned int age;
char tel[16];
float scores[3];
char sex;
};
void main()
{
struct students stu[2];
for (int i = 0; i < 2; i++) {
printf("姓名,年龄,电话,语文成绩,数学成绩,英语成绩,性别\n");
scanf("%s", stu[i].name);
scanf("%d", &stu[i].age);
scanf("%s", stu[i].tel);
scanf("%f", &stu[i].scores[0]);
scanf("%f", &stu[i].scores[1]);
scanf("%f", &stu[i].scores[2]);
scanf("%c", &stu[i].sex);
rewind(stdin);
}
for (int i = 0; i < 2; i++) {
printf("%s\n", stu[i].name);
printf("%d\n", stu[i].age);
printf("%s\n", stu[i].tel);
printf("%.1f\n", stu[i].scores[0]);
printf("%.1f\n", stu[i].scores[1]);
printf("%.1f\n", stu[i].scores[2]);
printf("%s\n", stu[i].sex == 'M' ? "男" : "女");
}
system("pause");
}
//printf("%d\n", BUFSIZ);
02 问题:scanf输入所有信息,第2个scanf时出错
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct students
{
char name[21];
unsigned int age;
char tel[16];
float scores[3];
char sex;
};
void main()
{
struct students stu[2];
for (int i = 0; i < 2; i++) {
printf("姓名,年龄,电话,语文成绩,数学成绩,英语成绩,性别\n");
scanf("%s%d%s%f%f%f%c", stu[i].name, &stu[i].age, stu[i].tel, &stu[i].scores[0], &stu[i].scores[1], &stu[i].scores[2], &stu[i].sex);
}
for (int i = 0; i < 2; i++) {
printf("%s\n", stu[i].name);
printf("%d\n", stu[i].age);
printf("%s\n", stu[i].tel);
printf("%.1f\n", stu[i].scores[0]);
printf("%.1f\n", stu[i].scores[1]);
printf("%.1f\n", stu[i].scores[2]);
printf("%s\n", stu[i].sex == 'M' ? "男" : "女");
}
system("pause");
}
02 01 原因:%c读取了空格’F’,导致后面的顺延乱码
02 02 方法一:getchar()吃掉\n
02 03 方法二:rewind(stdin)清空缓存区
02 04 方法三:规定输入,
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct students
{
char name[21];
unsigned int age;
char tel[16];
float scores[3];
char sex;
};
void main()
{
struct students stu[2];
for (int i = 0; i < 2; i++) {
printf("姓名,年龄,电话,语文成绩,数学成绩,英语成绩,性别\n");
scanf("%s%d%s%f%f%f,%c", stu[i].name, &stu[i].age, stu[i].tel, &stu[i].scores[0], &stu[i].scores[1], &stu[i].scores[2], &stu[i].sex);
}
for (int i = 0; i < 2; i++) {
printf("%s\n", stu[i].name);
printf("%d\n", stu[i].age);
printf("%s\n", stu[i].tel);
printf("%.1f\n", stu[i].scores[0]);
printf("%.1f\n", stu[i].scores[1]);
printf("%.1f\n", stu[i].scores[2]);
printf("%s\n", stu[i].sex == 'M' ? "男" : "女");
}
system("pause");
}
03 BUFSIZ 512B 缓冲区大小
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct students
{
char name[21];
unsigned int age;
char tel[16];
float scores[3];
char sex;
};
void main()
{
struct students stu[1];
for (int i = 0; i < 1; i++) {
printf("姓名,年龄,电话,语文成绩,数学成绩,英语成绩,性别\n");
scanf("%s", stu[i].name);
scanf("%d", &stu[i].age);
scanf("%s", stu[i].tel);
scanf("%f", &stu[i].scores[0]);
scanf("%f", &stu[i].scores[1]);
scanf("%f", &stu[i].scores[2]);
scanf("%*c", &stu[i].sex);
printf("%d\n", BUFSIZ);
}
for (int i = 0; i < 1; i++) {
printf("%s\n", stu[i].name);
printf("%d\n", stu[i].age);
printf("%s\n", stu[i].tel);
printf("%.1f\n", stu[i].scores[0]);
printf("%.1f\n", stu[i].scores[1]);
printf("%.1f\n", stu[i].scores[2]);
printf("%s\n", stu[i].sex == 'M' ? "男" : "女");
}
system("pause");
}