当前位置: 技术问答>linux和unix
宏_FILE_和_LINE_的问题.
来源: 互联网 发布时间:2015-03-12
本文导语: /*filefcn.h*/ #ifdef FILEFCN_H_ #define FILEFCN_H_ int open_file(FILE **fp,char *fname, char *mode,int line,char *file); #endif /* filefcn.c */ #include #incude "filefcn.h" int open_file(FILE **fp,char *fname, char *mode,int line,char *file) { if((*fp = open...
/*filefcn.h*/
#ifdef FILEFCN_H_
#define FILEFCN_H_
int open_file(FILE **fp,char *fname, char *mode,int line,char *file);
#endif
/* filefcn.c */
#include
#incude "filefcn.h"
int open_file(FILE **fp,char *fname, char *mode,int line,char *file)
{
if((*fp = open(fname,mode))==NULL){
fprintf(stderr,"[%s:%d] open_file() failedn",file,line);
return 1;}
}
/*testmacs.c*/
#include
#include
#include "filefcn.h"
int main(void)
{
FILE *fp;
/*this call will work*/
if(open_file(&fp,"foo_bar","w",_LINE_,_FILE_))
{
exit(EXIT_FAILURE);}
else
{
fputs("this text proves we scribbld in the file.n",fp);
fclose(fp);}
/* this call will fail*/
if(open_file(&fp,"bar_baz","r",_LINE_,_FILE_))
{
exit(EXIT_FAILURE);}
else
{
fclose(fp);}
return 0;
}
编译的时候:
testmacs.c: In function `main':
testmacs.c:12: `_LINE_' undeclared (first use in this function)
testmacs.c:12: (Each undeclared identifier is reported only once
testmacs.c:12: for each function it appears in.)
testmacs.c:12: `_FILE_' undeclared (first use in this function)
filefcn.c: In function `open_file':
filefcn.c:7: warning: assignment makes pointer from integer without a cast
为什么没有定义呢.难道还需要定义吗.
#ifdef FILEFCN_H_
#define FILEFCN_H_
int open_file(FILE **fp,char *fname, char *mode,int line,char *file);
#endif
/* filefcn.c */
#include
#incude "filefcn.h"
int open_file(FILE **fp,char *fname, char *mode,int line,char *file)
{
if((*fp = open(fname,mode))==NULL){
fprintf(stderr,"[%s:%d] open_file() failedn",file,line);
return 1;}
}
/*testmacs.c*/
#include
#include
#include "filefcn.h"
int main(void)
{
FILE *fp;
/*this call will work*/
if(open_file(&fp,"foo_bar","w",_LINE_,_FILE_))
{
exit(EXIT_FAILURE);}
else
{
fputs("this text proves we scribbld in the file.n",fp);
fclose(fp);}
/* this call will fail*/
if(open_file(&fp,"bar_baz","r",_LINE_,_FILE_))
{
exit(EXIT_FAILURE);}
else
{
fclose(fp);}
return 0;
}
编译的时候:
testmacs.c: In function `main':
testmacs.c:12: `_LINE_' undeclared (first use in this function)
testmacs.c:12: (Each undeclared identifier is reported only once
testmacs.c:12: for each function it appears in.)
testmacs.c:12: `_FILE_' undeclared (first use in this function)
filefcn.c: In function `open_file':
filefcn.c:7: warning: assignment makes pointer from integer without a cast
为什么没有定义呢.难道还需要定义吗.
|
均少了一个下划线, 应该是 __LINE__和__FILE__