本文介绍了strto [u] l和ERANGE的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! strtol和strtoul被定义为将errno设置为ERANGE,如果他们得到 输入表示数字太大或太小而不能代表 长或无符号长( n869 7.20.1.4#8)。 这是否意味着这个(来自n869 7.5): -------- [#3] errno的值在程序启动时为零,但是 从未被任何库函数设置为零.159) $ b $的值b errno可以通过库函数调用设置为非零 是否有错误,前提是使用errno ^^^^^^^^^ ^^^^^^^^^^^^^^^^^ 未在此功能描述中记录 ^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^ 国际标准。 -------- 禁止strtol和朋友将errno设置为ERANGE如果输入 表示允许范围内的值? 如果他们被允许设置错误o ERANGE对于任何输入,是否有任何 合理的库实现实际上这样做,或者这个 代码仍然可以安全使用? -------- errno = 0; value = strtol(str,& endptr,10); if(errno == ERANGE) { whine_and_handle_error(" strtol set ERANGE"); } if(endptr == str) { whine_and_handle_error(strtol找不到值); } / *继续... * / -------- 或者我还需要检查价值是否为1 fgets 的值是否超出范围输入? (在这种情况下,是否有任何保证 方式来区分最大值和太大之间的区别?) dave - Dave Vandervies dj******@csclub.uwaterloo.ca 我已经阅读了所有相关的男人。页面上,在b $ b终端上撒上鸡血,并说出所有相应的诅咒。所以现在是时候从更强大的力量寻求帮助了。 --Peter A. Buhr in uw.mfcf.gripestrtol and strtoul are defined as setting errno to ERANGE if they getinput representing a number too large or too small to represent in along or unsigned long (n869 7.20.1.4#8).Does this mean that this (from n869 7.5):--------[#3] The value of errno is zero at program startup, but isnever set to zero by any library function.159) The value oferrno may be set to nonzero by a library function callwhether or not there is an error, provided the use of errno^^^^^^^^^^^^^^^^^^^^^^^^^^is not documented in the description of the function in this^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^International Standard.--------prohibits strtol and friends from setting errno to ERANGE if their inputrepresents a value within the allowed range?If they''re allowed to set errno to ERANGE for any input, are there anyreasonable library implementations that actually do this, or would thiscode still be safe to use?--------errno=0;value=strtol(str,&endptr,10);if(errno==ERANGE){whine_and_handle_error("strtol set ERANGE");}if(endptr==str){whine_and_handle_error("strtol couldn''t find value");}/*Carry on...*/--------Or would I also need to check for value being one of the values that fgetsreturned on out-of-range input? (In that case, is there any guaranteedway to tell the difference between maximum-value and too-large?)dave--Dave Vandervies dj******@csclub.uwaterloo.caI have read all the pertinent "man" pages, sprinkled chicken blood on theterminal, and uttered all the appropriate curses. So it is time to seek helpfrom a greater power. --Peter A. Buhr in uw.mfcf.gripe推荐答案 是的。虽然strtoul有一个警告,因为大多数b 实现 读取标准为接受负值。因此输入-1将 产生UINT_MAX而不设置errno。 %类型strtoul.c #包括< errno.h> #include< stdio.h> #include< stdlib.h> int main(void) { unsigned u = strtoul(" -1",0,10); printf(" ;%u [%d] \ n",u,errno); 返回0; } % gcc -ansi -pedantic strtoul.c %a.exe 4294967295 [0] % - 彼得Yes. Although there is a caveat with strtoul in that mostimplementationsread the standard as accepting negative values. So input of ''-1'' wouldproduce UINT_MAX without setting errno.% type strtoul.c#include <errno.h>#include <stdio.h>#include <stdlib.h>int main(void){unsigned u = strtoul("-1", 0, 10);printf("%u [%d]\n", u, errno);return 0;}% gcc -ansi -pedantic strtoul.c% a.exe4294967295 [0]%--Peter 我没有最微弱的关于价值的意思fgets() 返回超出范围的输入,因为fgets()没有概念在范围内或超出范围。 也许你应该再次发帖并重新发布你的帖子几次 发送。 - Jack Klein 主页: http:// JK -Technology.Com 常见问题解答 comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html comp.lang.c ++ http://www.parashift.com/c++- faq-lite / alt.comp.lang.learn.c-c ++ http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.htmlI have not got the faintest idea of what you mean about values fgets()returns on out-of-range input, since fgets() has no concept of inrange or out of range.Perhaps you should post again and reread your post a few times beforesending.--Jack KleinHome: http://JK-Technology.ComFAQs forcomp.lang.c http://www.eskimo.com/~scs/C-faq/top.htmlcomp.lang.c++ http://www.parashift.com/c++-faq-lite/alt.comp.lang.learn.c-c++ http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html 这是正确的吗?根据我对7.20.1.4的读数(引用时间太长) $ stboul(-1,0,10)中的,第一个1。将转换为(数学)值1。然后,因为有一个 - 的b $ b,所以该值被否定(值-1)。然后检查 如果值是可表示的(0..ULONG_MAX);显然不是, 所以strtoul应该返回ULONG_MAX并将'errno''设置为ERANGE。 我有什么问题吗? - Stan Tobias mailx`echo si ** *@FamOuS.BedBuG.pAlS.INVA LID | sed s / [[:upper:]] // g`Is that correct? According to my reading of 7.20.1.4 (too long to quote)in `strtoul("-1", 0, 10)'', first "1" is convertedinto a (mathematical) value `1''. Then, since there''sa "-", the value is negated (value `-1''). Then a check is madeif the value is representable (0..ULONG_MAX); it is clearly not,so strtoul should return ULONG_MAX and set `errno'' to ERANGE.Have I got something wrong?--Stan Tobiasmailx `echo si***@FamOuS.BedBuG.pAlS.INVALID | sed s/[[:upper:]]//g` 这篇关于strto [u] l和ERANGE的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
09-05 06:14
查看更多