gpt4 book ai didi

c - 使用 strtok 验证用户在 C 中输入的日期的问题

转载 作者:行者123 更新时间:2023-11-30 19:41:41 26 4
gpt4 key购买 nike

我正在尝试验证用户在 C 中输入的日期。但是,我似乎无法让 strtok 用“-”分隔符分隔。例如,我想验证用户是否以 mm/dd/yyyy-hh:mm 格式正确输入了日期,其中 hh 是小时的 2 位数表示形式,mm 也是 2 位数的小时过后的分钟数。

这就是我一直在做的事情。

 char* input[] //--user input

char* token[];//-- used to seperate each portion and verify them individually.

token = strtok(input, " /-:"); //-- token is now the mm.
...todo

token = strtok(NULL, "/"); //---token is now the dd.
...todo

token = strtok(NULL, "/"); //---token is now the yyyy.
...todo

我每一步都使用 prinf token 进行验证,一切都运行顺利,直到我尝试到达 hh。

问题是这样的:当我尝试打电话时

token = strtok(NULL, "-") //for the hours in a valid input

token 为 NULL。在待办事项中,我验证 token 不为空,但在这种情况下,即使我输入有效日期,它也始终为空。

最佳答案

您可能会考虑使用

if ( sscanf ( input, "%d/%d/%d-%d:%d", &mm, &dd, &yyyy, &hr, &m) == 5)

格式字符串扫描一个int、一个'/'、一个int、一个'/'、一个int、一个'-'、一个 int、一个 ':' 和一个 int。如果成功扫描所有项目,则返回5。

关于c - 使用 strtok 验证用户在 C 中输入的日期的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33266656/

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