#ifndef __errmacros_h__
#define __errmacros_h__
#include <errno.h>
#ifdef DEBUG
#define DEBUG_PRINT(...) \
do { \
printf("In %s - function %s at line %d: ", __FILE__, __func__, __LINE__); \
printf(__VA_ARGS__); \
} while(0)
#else
#define DEBUG_PRINT(...) (void)0
#endif
#define ERROR_HANDLER(err) \
do { \
if ( (err) != 0 ) \
{ \
printf("In %s - function %s at line %d: ", __FILE__, __func__, __LINE__); \
perror("Error happens"); \
exit( EXIT_FAILURE ); \
} \
} while(0)
#define SBUFFER_ERROR(err) \
do { \
if ( (err) != 0 ) \
{ \
printf("In %s - function %s at line %d: ", __FILE__, __func__, __LINE__); \
printf("Sbuffer operation error"); \
exit( EXIT_FAILURE ); \
} \
} while(0)
#define SYSCALL_ERROR(err) \
do { \
if ( (err) == -1 ) \
{ \
perror("Error executing syscall"); \
exit( EXIT_FAILURE ); \
} \
} while(0)
#define CHECK_MKFIFO(err) \
do { \
if ( (err) == -1 ) \
{ \
if ( errno != EEXIST ) \
{ \
perror("Error executing mkfifo"); \
exit( EXIT_FAILURE ); \
} \
} \
} while(0)
#define FILE_OPEN_ERROR(fp) \
do { \
if ( (fp) == NULL ) \
{ \
perror("File open failed"); \
exit( EXIT_FAILURE ); \
} \
} while(0)
#define FILE_CLOSE_ERROR(err) \
do { \
if ( (err) == -1 ) \
{ \
perror("File close failed"); \
exit( EXIT_FAILURE ); \
} \
} while(0)
#define FILE_SCANF_ERROR(err) \
do { \
if ( (err) == -1 ) \
{ \
perror("File scanf failed"); \
exit( EXIT_FAILURE ); \
} \
} while(0)
#define FILE_PUTS_ERROR(err) \
do { \
if ( (err) == -1 ) \
{ \
perror("File fputs failed"); \
exit( EXIT_FAILURE ); \
} \
} while(0)
#define ASPRINTF_ERROR(err) \
do { \
if ( (err) == -1 ) \
{ \
perror("asprintf failed"); \
exit( EXIT_FAILURE ); \
} \
} while(0)
#define FFLUSH_ERROR(err) \
do { \
if ( (err) == EOF ) \
{ \
perror("fflush failed"); \
exit( EXIT_FAILURE ); \
} \
} while(0)
#endif
使用举例:DEBUG_PRINT("output is %s,%d",output_buf,40)..或者DEBUG_PRINT("output error")
二,assert函数
需要引入#include <assert.h> void assert(int expression)
当expression正确的时候不执行任何动作,如果expression为假则会在标准错误stderr上打印错误信息,并终止程序
#include <assert.h>
#include <stdio.h>
int main()
{
int a;
char str[50];
printf("请输入一个整数值: ");
scanf("%d", &a);
assert(a >= 10);
printf("输入的整数是: %d\n", a);
printf("请输入字符串: ");
scanf("%s", str);
assert(str != NULL);
printf("输入的字符串是: %s\n", str);
return(0);
}
三、 perror函数
perror ( )用 来 将 上 一 个 函 数 发 生 错 误 的 原 因 输 出 到 标 准 设备 (stderr) 。参数 s 所指的字符串会先打印出,后面再加上错误原因字符串
#include <stdio.h>
//#include <stdlib.h>
int main(void)
{
FILE *file;
file=fopen("./root","r+");
if(file==NULL)
{
perror("./root");
}
return 0;
}
打印输出 ./root: No such file or directory
四,linux下的三种流,stdin,stdout,stderr
这里主要记录stdout和stderr,标准输出stdout, 标准输出的文件标识符为1
#include <stdio.h>
intmain(void)
{
printf("%s\n", "hello");
fprintf(stdout, "%s\n", "hello");
return0;
}
两个hello会在屏幕上输出
stderr举例,标准错误输出的文件标识符为2
#include <stdio.h>
int main(void)
{
printf("%s\n", "hello");
fprintf(stderr, "%s\n", "error");
return 0;
}
也会在屏幕上输出打印信息 hello和error
从上边看有什么意义呢stderr 接下来看
#include <stdio.h>
int main(void)
{
printf("%s\n", "hello");
fprintf(stderr, "%s\n", "error");
return 0;
}
执行 ./test 1>log.txt 2>error.txt 这样error就打印到error.txt中,而hello就打印在log.txt中。
在c语言里,也可以使用freopen函数重定向输出流。
#include <stdio.h>
int main(void)
{
FILE*out = freopen("out.txt", "w", stdout);
printf("%s\n", "hello");
return 0;
}
上边代码编译后,输出就打印到out.txt中了。
五、strerror函数
这个函数的作用效果其实和上边的差不多
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
int main(void)
{
int fd = 10;
int ret;
ret = close(fd);
if(ret == -1)
fprintf(stderr, "close error with msg is: %s\n",strerror(errno));
return 0;
}
errno是#include <error.h>中的全局变量,效果如下