验证输入 cmdline 输入 argv[] 包含所有整数
所以我有一个处理数字操作的作业,其中还包括错误检查。我在错误检查方面遇到问题。用户通过命令行并提供 8 个以空格分隔的数字来使用该应用程序。我在验证提供的数据实际上是整数时遇到问题。
建议我使用 strtol() 方法,但是我知道如果整数无效,它会返回 0,但我需要返回错误消息而不是 0,因为 0 是有效的。我可以使用另一种方法来验证输入吗?
So I have an assignment dealing with number manipulation that also includes error checking. I'm having issues with the error checking side. A user uses the application by via commandline and giving 8 numbers that are space separated. I am having a problem validating that the data provided are actually integers.
I was suggested to use the method strtol() however I know that if the integer is invalid, it returns a 0, but I need to return an error message instead of a 0 because 0 is valid. Is there another method I can use to validate input?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
strtol
不仅有其返回值来指示转换中的错误,它还有第二个参数(我的手册页中的endptr
);如果您向其传递一个指向 char * 的指针,它将存储无法转换的第一个字符的位置,或者如果无法转换任何内容,则将其保留。因此,您会遇到以下情况:此外,您还可以检查
strtol
设置errno
的值,以防出现问题;如果无法提取任何内容,则使用EINVAL
,其他值可以在strtol
的联机帮助页上看到。您还可以使用 sscanf 并检查其返回值,以快速查看字符串是否可以转换为 int(或您在格式字符串中设置的任何内容)。
strtol
do not have only its return value to signal an error in the conversion, it has also its second parameter (endptr
in my manpage); if you pass to it a pointer to achar *
, it will store there the position of the first character that it couldn't convert, or will leave it alone if nothing could be converted. Thus, you have the following cases:Moreover, you can also check the value to which
strtol
setserrno
in case of problems;EINVAL
is used if nothing could be extracted, the other values can be seen on the manpage ofstrtol
.You can also use
sscanf
and check its return value to quickly see if the string could or could not be converted toint
(or to whatever you set in the format string).如果
strtol()
遇到错误,它会将errno
设置为EINVAL
。从 手册页:If
strtol()
encounters an error, it will seterrno
toEINVAL
. From the man page:命令行参数是字符串,为什么不使用 <代码>isdigit(3)?
The command line arguments are strings, why not use
isdigit(3)
?要正确使用
strtol
,您必须在调用它之前重置errno
。如果您编写想要在其他项目中重用的代码,则该代码不应产生意外的副作用。因此有两种变体:一种简单的变体可能适合您的情况,另一种复杂的变体在可重用代码库中可以接受。使用这些函数之一来检查你的参数应该很容易,所以我把这部分留给你去发现。
To use
strtol
properly, you have to reseterrno
before calling it. And if you write code that you want to reuse in other projects, that code should have no unintended side-effects. So there are two variants: a simple one that is probably good enough in your case, and a complex one that would be acceptable in a library of reusable code.Using one of these functions for checking your arguments should be quite easy, so I leave that part to you to find out.