如何设置errno值? [英] How to set errno value?
问题描述
我有两个调用两种不同的方法:
无效func1的()
{
// 做一点事
如果(故障)
{
//然后设置errno为EEXIST }}
和第二个方法:
无效FUNC2()
{
// 做一点事
如果(故障)
{
//然后设置errno为ENOENT }}
-
当我设置了
错误号
来一定的价值,它有什么作用?只是错误检查? -
如何设置
错误号
在上述方法FUNC1
和FUNC2
到EEXIST
和ENOENT
感谢
对于所有实用的目的,你可以把错误号
就像一个全局变量(尽管它通常不是)。因此包括 errno.h中
,只是使用它:
错误号= ENOENT;
您应该问自己,如果错误号
是为您的目的最好的错误报告机制。功能可被改造成返回错误code自己呢?
I have two calls to two different methods :
void func1()
{
// do something
if (fail)
{
// then set errno to EEXIST
}
}
And the second method :
void func2()
{
// do something
if (fail)
{
// then set errno to ENOENT
}
}
When I set the
errno
to some value , what does it do ? just error checking ?How can I set
errno
in the above methodsfunc1
andfunc2
toEEXIST
andENOENT
Thanks
For all practical purposes, you can treat errno
like a global variable (although it's usually not). So include errno.h
and just use it:
errno = ENOENT;
You should ask yourself if errno
is the best error-reporting mechanism for your purposes. Can the functions be engineered to return the error code themselves ?
这篇关于如何设置errno值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!