如何将错误添加到perror堆栈?
下面是我想要的一个示例
#include <stdio.h>
#include <stdlib.h>
int Div (int a, int b, int * c) {
if (b == 0) {
// add to perror: "cannot divide by zero!"
return 0;
}
*c = a / b;
return 1;
}
int main () {
int n;
if (!Div(2, 0, &n)) {
perror("could not divide");
}
return 1;
}发布于 2012-04-22 12:08:55
在我所知的系统中,没有标准的(或非标准的)方法来添加新的errno值;您可以赋值给errno以使用现有的值,但对于不属于标准库的任何内容,这都不是一个好主意。
发布于 2012-04-22 12:28:52
唯一的方法是更改C标准库,而您并不想这样做。
如果您更改libc并使用修改后的errno编号,则可以添加您自己的编号。但是,你的程序只能在你修改过的“标准”库的系统上正常工作。
https://stackoverflow.com/questions/10265129
复制相似问题