gpt4 book ai didi

c - 读取字符串并在结构中存储为 int 的最安全方法

转载 作者:太空宇宙 更新时间:2023-11-04 03:11:34 25 4
gpt4 key购买 nike

我在 gcc 编译器上使用 ANSI C(带有 -ansi)。

我需要将月、日、小时和分钟的用户输入读入一个结构中,他们:

  • 不能是非 int 的数据类型,
  • 每个人都需要符合各自的标准(i.e. month > 0 && month <
    13
    等)

结构定义

typedef struct 
{
int month;
int day;
int hour;
int minute;
} date_time_t;

date_time_t departure_date[50];

使用字符串转换进行类型检查

如果他们向 scanf("%i", departure_date->month); 提供“~”,我想检查用户输入以阻止程序崩溃

所以我首先像这样读取值作为字符串:

char temp_month[3]
char *ptr;
scanf("%s", temp_month)

然后像这样对用户输入进行类型检查:

当输入不符合条件时 -> 要求输入符合条件

 while(strtol(temp_month,  &ptr, 36) <  1 ||
strtol(temp_month, &ptr, 36) > 12)
{

printf("Invalid selection - try again\n");
scanf(" %s", temp_month);
}

一旦满足 while 条件,我将临时变量存储在结构中:

departure_date->month = atoi(temp_month);

几个问题...

  1. 这是正常的做事方式吗?请记住,我受限于只有 int 数据类型的结构。
  2. 当我在 scanf 期间将击键“a、b、c 或 d”提交给月份时,它通过了我正在进行类型检查的 while 循环设定的标准,但字母表中的其他字母没有这样做 - 确实有人知道为什么吗?

最佳答案

typedef int error;
#define SUCCESS 0
#define FAILURE 1

error readdate(date_time_t *dest)
{
char line[80];
if (fgets(line, sizeof line, stdin) == NULL)
return FAILURE;

if (sscanf(line, "%d %d %d %d", &(dest->month), &(dest->day), &(dest->hour),
&(dest->minute)) == 4 && dest->month > 0 && dest->month < 13
&& dest->day > 0 && dest->day < 32 && dest->hour > -1 &&
dest->hour < 25 && dest->minute > 0 && dest->minute > 60)

return SUCCESS; /* the return value of sscanf is equal to the number
of objects successfully read in and converted; also we
check the bounds of the input */
return FAILURE;
}

我们使用 fgets 然后使用 sscanf 而不是仅仅使用 scanf 以避免刷新标准输入流时出现任何问题。

scanf 函数系列返回成功读入并转换为给定数据类型的对象数。

这个函数的主要问题是它不向调用者报告遇到的错误类型;它仅表示发生了某种错误,或者没有发生错误。

关于c - 读取字符串并在结构中存储为 int 的最安全方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55853238/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com