C语言对于输入数据截断类型的非法输入问题的解决办法


前言

前些时候在做 c 语言课程设计,对于输入数据截断类型的非法输入问题总结出了一个很好的解决办法,在此分享。


一、函数实现

代码如下:

#include<stdio.h>
#include<string.h>
void getinput(char* destination, int limit_characters_number)
{
	if(sizeof(destination) < limit_characters_number + 1)
		limit_characters_number = sizeof(destination) - 1;
	memset(destination, 0, sizeof(destination));
	rewind(stdin);
	fgets(destination, limit_characters_number + 1, stdin);
	rewind(stdin);
	if ('\n' == destination[strlen(destination) - 1]) 
		destination[strlen(destination) - 1] = 0;
	return;
}

二、函数分析

1.整体分析

#include<stdio.h>											 //包含所用到的 rewind() 和 fgets() 函数原型
#include<string.h>											 //包含所用到的 strlen() 函数原型
void getinput(char* destination, int limit_characters_number)//需要传入 目标字符数组名 限制字符长度;无 返回值
{
	if(sizeof(destination) < limit_characters_number)		 //如果 limit_characters_number + 1 超过 destination 字符数组实际大小
		limit_characters_number = sizeof(destination) - 1;	 //那么将 limit_characters_number 赋值为实际可接收的有效字符个数
	memset(destination, 0, sizeof(destination));			 //重新初始化字符数组
	rewind(stdin);											 //清空 标准输入缓冲区
	fgets(destination, limit_characters_number + 1, stdin);	 //将 limit_characters_number 个字符从 stdin 保存至 destination 中
	rewind(stdin);											 //清空 标准输入缓冲区
	if ('\n' == destination[strlen(destination) - 1]) 		 //如果 destination 的最后一个字符是回车字符 '\n'
		destination[strlen(destination) - 1] = 0;			 //那么清除回车字符 '\n'
	return;
}

2.具体讲解

char* destination

最开始我是使用正常的数据处理方式:数字使用 int ,double 等类型,字符使用 char,char[] 类型,但是在数字类型方面对非法数据的处理毫无头绪!因此,我将程序内的数据一律改为 char[] 字符数组的方式进行保存,便使得所有的数据输入都可以使用统一函数进行处理。

建议在即使输入一个字符情况下,也将字符数组开辟 3 个字符的空间,这样既可以保证给后面所讲到的 fgets() 函数处理尾字符留出位置,又能对超出单个字符长度但缺仍能视为合法输入的数据的情况进行判别。

比如我们需要判断输入是否为 c 从而取消当前功能,当输入如下时:

cdfgbdshyfdgnh

我们可以通过判断字符数组的第二个字符是否等于 '\0' 来避免上述数据被误判为合法输入。

在后期在对数字类型数据的处理上可能会出现问题,这里展示一些对于字符数组与数字类型相互转化的函数,方便大家使用。

函数名函数原型函数功能头文件
atoi()int atoi(const char* nptr) ;将字符串转换为整型数stdlib.h
strtod()double strtod(const char *nptr,char **endptr);将字符数组转换为浮点数stdlib.h
itoa()char* itoa (int value, char* str, int base );将整型数转换为字符数组stdlib.h
ecvt()char* ecvt(double value, int ndigit, int *decpt, int *sign);将双精度浮点数转换为字符数组stdlib.h
memset(destination, 0, sizeof(destination));

将字符数组重新初始化为 0。

因为我在一些输入为非法输入时会提示重新输入,而上一次的数据可能会影响重新输入后的数据。

rewind(stdin);

首先我们需要知道,从 C11 开始 C 语言就不再支持使用 fflash(stdin) 来清空输入缓冲区了,大家可以看一下《C 清空输入缓冲区,以及 fflush(stdin) 的使用误区和解决方法》这篇文章,了解更多。

相应的,我找到 rewind(stdin) 进行替代:
rewind() 的函数原型为 void rewind(FILE *fp);,其作用是使文件指针 fp 的位置指向文件启示位置。而这里使用的 rewind(stdin) ,就是将输出输入缓冲区的指针指向起始位置,也就相当于清空了输入缓冲区。

对应代码部分,fgets() 前一个 rewind(stdin) 是保险措施,如果在这次输入之前缓冲区仍有数据,则需要清除。而后一个 rewind(stdin) 则当输入数据长度超过 fgets() 截取的长度后对剩余数据进行清除。

在实际应用中,如果所有的输入处理都使用本函数,则可以省去第一个 rewind(stdin)

fgets(destination, limit_characters_number + 1, stdin);

fgets() 的函数原型为:char *fgets(char *str, int n, FILE *stream);,其功能为从指定的流 stream 读取一行,并把它存储在 str 所指向的字符串内。当读取 (n-1) 个字符时,或者读取到换行符时,或者到达文件末尾时,它会停止,具体视情况而定。
也就是说,当我们想要限制读取 n 个字符时,我们需要传入的参数 应为 n+1 ,因为 fgets() 会将第 n+1 个此字符留给 '\0',这就是为什么 limit_characters_number + 1 的原因。

if (’\n’ == destination[strlen(destination) - 1]) destination[strlen(destination) - 1] = 0;

这个应该很好理解,因为 fget() 函数会将 '\n' 也保存到字符数组中,所以我们必须清除可能存在于字符数组内有效字符最后的 '\n',防止后期处理字符数组时产生问题。

而函数中的第一句 if(sizeof(destination) < limit_characters_number + 1)limit_characters_number = sizeof(destination) - 1; 则是处理了当传入的限制字符长度超过了字符数组的长度的情况。


总结

整个函数的功能实现主要为 rewind(stdin) 对缓冲区的清空 以及 fgets() 函数对输入字符限制作用。

其他

在此推荐一波我的 c 课设 航班预定管理系统 gitee 仓库。

其中未使用本篇文章所使用的函数,但是因为当时想把这些代码组成函数时,我已经将大部分输入处理都改成现在仓库中的形式,再要改成函数,改动量过大,就没有使用。

Thx for reading!

  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值