C++ 带有参数的宏定义


宏定义中 多行时用"\"换行。

  1. #define CHECKFILE(state, path) \
  2. if (!state) \
  3. {\
  4. fprintf(stderr, "At file %s, line %d: \nFile open error: %s\n", __FILE__, __LINE__, path); \
  5. exit(-1);\
  6. }\

fprintf是C/C++中的一个格式化写—库函数;其作用是格式化输出到一个流/文件中;函数完整形式: int fprintf(FILE *stream,char *format [,argument])

功 能
  传送格式化输出到一个文件中

用 法
  #include <stdio.h>
  int fprintf( FILE *stream, const char *format, ... );
  fprintf()函数根据指定的format(格式)(格式)发送信息(参数)到由stream(流)指定的文件. fprintf()只能和printf()一样工作. fprintf()的返回值是输出的字符数,发生错误时返回一个负值.

返回值
  若成功则返回输出字符数,若输出出错则返回负值。
fprintf使用stderr参数会打印到屏幕上。
fprintf 函数的功能是: Print formatted data to a stream

格式化输出数据到流,这个流并没有特指是文件流。

关于 stdin、stdout、stderr 的说明如下:

By default, standard input is read from the keyboard, while standard output and standard error are printed to the screen

默认情况下,标准输入从键盘读取,同时标准输出和标准错误会打印到屏幕。

在控制台测试 :

  1. void main( void
  2.   fprintf(stderr, "%s:%d", __FILE__, __LINE__); 
  3.   system("pause"); 

会在屏幕显示当前文件的路径和fprintf语句所在的行数。

相关内容