我一直在想以C錯誤處理所產生的困難..像究竟是誰呢在C
if(printf("hello world")==-1){exit(1);}
但你不這樣做,例如冗長,而且通常無用打破通用標準編碼。那麼如果你有一個libc的包裝呢?像這樣,你可以做這樣的事情..
//main...
error_catchall(my_errors);
printf("hello world"); //this will automatically call my_errors on an error of printf
ignore=1; //this makes it so the function will return like normal and we can check error values ourself
if(fopen.... //we want to know if the file opened or not and handle it ourself.
}
int my_errors(){
if(ignore==0){
_exit(1); //exit if we aren't handling this error by flagging ignore
}
return 0;
//this is called when there is an error anywhere in the libc
}
...
我正在考慮作出這樣的包裝,因爲我綜合我自己的BSD許可的libc(所以我已經有觸摸碰不得..),但我想知道人們對它的看法。 這實際上是否會在現實生活中起作用,並且比返回-1更有用?
檢查printf(...)== -1是沒有用的,因爲printf(...)返回打印的字符數並且不能小於0. – Kai 2009-06-04 18:06:41
「打印的字符數返回。如果發生錯誤,則返回-1。「 – Earlz 2009-06-04 18:11:21