出错处理之二——使用预编译宏__LINE__ 和 __FILE__
来源:互联网 发布:仿微信源码 编辑:程序博客网 时间:2024/04/28 08:46
摘要:上一节(http://blog.chinaunix.net/u/16292/showart_391324.html)中,
我们说明了assert函数的用法,除此之外,标准C还定义了两个宏:__LINE__ 和 __FILE__,
它们可以和assert联合起来用以定位更精确的程序bug点。
下面我们通过一个例子来说明__LINE__ 和 __FILE__的用法。
zieckey@yahoo.com.cn
程序清单1:
/*
* filefcn.h - A function to open files.
*/
#ifndef FILEFCN_H_
#define FILEFCN_H_
int open_file( FILE **fp, char *fname, char *mode, int line, char *file );
#endif/* FILEFCN_H_ */
程序清单2:
/*
* filefcn.c - A function to open files,using __LINE__ and __FILE__
*/
#include <stdio.h>
#include "filefcn.h"
int open_file( FILE **fp, char *fname, char *mode, int line, char *file )
{
if ( NULL == (*fp = fopen(fname, mode)) )
{
fprintf( stderr, "[%s:%d] open_file() failed.\n", file ,line );
return 1;
}
return 0;
}
程序清单3:
/*
* testmacros.c - Exercise the function defined in filefcn.c
*/
#include <stdio.h>
#include <stdlib.h>
#include "filefcn.h"
int main(void)
{
FILE *fp;
if ( open_file( &fp, "test.txt", "w", __LINE__, __FILE__ ) )
{
return EXIT_FAILURE;
}
else
{
fputs( "This is the text proves we scribbled in the file.\n", fp );
fclose( fp );
}
if ( open_file ( &fp, "noexitfile.txt", "r", __LINE__, __FILE__ ) )
{
return EXIT_FAILURE;
}
else
{
fclose( fp );
}
return 0;
}
编译运行:
[root@localhost error_process]# gcc testmacros.c filefcn.c
[root@localhost error_process]# ./a.out
[testmacros.c:23] open_file() failed.
[root@localhost error_process]#
在编译之前,预处理器把 __LINE__分别换成了 13 和 23,把__FILE__换成了源码文件的文件名 testmacros.c。
如果open_file函数调用成功,则返回0,否则打印出诊断信息。
这样我通过标准C的两个宏:__LINE__ 和 __FILE__实现了一个更健壮的fopen函数。
我们说明了assert函数的用法,除此之外,标准C还定义了两个宏:__LINE__ 和 __FILE__,
它们可以和assert联合起来用以定位更精确的程序bug点。
下面我们通过一个例子来说明__LINE__ 和 __FILE__的用法。
zieckey@yahoo.com.cn
程序清单1:
/*
* filefcn.h - A function to open files.
*/
#ifndef FILEFCN_H_
#define FILEFCN_H_
int open_file( FILE **fp, char *fname, char *mode, int line, char *file );
#endif/* FILEFCN_H_ */
程序清单2:
/*
* filefcn.c - A function to open files,using __LINE__ and __FILE__
*/
#include <stdio.h>
#include "filefcn.h"
int open_file( FILE **fp, char *fname, char *mode, int line, char *file )
{
if ( NULL == (*fp = fopen(fname, mode)) )
{
fprintf( stderr, "[%s:%d] open_file() failed.\n", file ,line );
return 1;
}
return 0;
}
程序清单3:
/*
* testmacros.c - Exercise the function defined in filefcn.c
*/
#include <stdio.h>
#include <stdlib.h>
#include "filefcn.h"
int main(void)
{
FILE *fp;
if ( open_file( &fp, "test.txt", "w", __LINE__, __FILE__ ) )
{
return EXIT_FAILURE;
}
else
{
fputs( "This is the text proves we scribbled in the file.\n", fp );
fclose( fp );
}
if ( open_file ( &fp, "noexitfile.txt", "r", __LINE__, __FILE__ ) )
{
return EXIT_FAILURE;
}
else
{
fclose( fp );
}
return 0;
}
编译运行:
[root@localhost error_process]# gcc testmacros.c filefcn.c
[root@localhost error_process]# ./a.out
[testmacros.c:23] open_file() failed.
[root@localhost error_process]#
在编译之前,预处理器把 __LINE__分别换成了 13 和 23,把__FILE__换成了源码文件的文件名 testmacros.c。
如果open_file函数调用成功,则返回0,否则打印出诊断信息。
这样我通过标准C的两个宏:__LINE__ 和 __FILE__实现了一个更健壮的fopen函数。
- 出错处理之二——使用预编译宏__LINE__ 和 __FILE__
- 预编译之__LINE__和__FILE__宏
- __FILE__和__LINE__ 使用
- 四个预编译宏:__FILE__,__LINE__,__DATE__,__TIME__
- __FILE__和__LINE__的使用
- gcc中用于预编译的宏(如__cplusplus , __func__ , __FILE__ , __LINE__ 等)
- 使用__FILE__和__LINE__定位错误
- 使用__FILE__和__LINE__定位错误
- 使用__FILE__和__LINE__定位错误
- 使用__FILE__和__LINE__定位错误
- 使用__FILE__和__LINE__定位错误
- 使用__FILE__和__LINE__定位错误
- 使用__FILE__和__LINE__定位错误
- 使用__FILE__和__LINE__定位错误
- 使用__FILE__和__LINE__定位错误
- 使用__FILE__和__LINE__定位错误
- 使用__FILE__和__LINE__定位错误
- 宏定义中的#exp和__FILE__, __LINE__
- cppwebbrower中获取frame中的源码(续)
- Warning: Some pins have incomplete I/O assignments. Refer to the I/O Assignment Warnings report for
- 进程与线程的区别
- 出错处理之一 assert 函数用法
- 在PL/SQL中使用DDL语句及系统控制语句
- 出错处理之二——使用预编译宏__LINE__ 和 __FILE__
- 关于TreeView的无限绑定
- Debian 参考手册
- ID 810394.1 --RAC and Oracle Clusterware Starter Kit and Best Practices
- java 匿名类
- java card平台详解(一)
- 出错处理之三——标准库函数perror用法(打印出错误原因信息字符串)
- 出错处理之四——abort_exit_atexit_strerror函数的使用
- short、int、long与byte之间的转换工具类