如何设置errno值? [英] How to set errno value?

查看:516
本文介绍了如何设置errno值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个调用两种不同的方法:

 无效func1的()
{
  // 做一点事
  如果(故障)
  {
    //然后设置errno为EEXIST  }}

和第二个方法:

 无效FUNC2()
{
  // 做一点事
  如果(故障)
  {
    //然后设置errno为ENOENT  }}


  1. 当我设置了错误号来一定的价值,它有什么作用?只是错误检查?


  2. 如何设置错误号在上述方法 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

  }

}

  1. When I set the errno to some value , what does it do ? just error checking ?

  2. How can I set errno in the above methods func1 and func2 to EEXIST and ENOENT

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆