歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> C++ 帶有參數的宏定義

C++ 帶有參數的宏定義

日期:2017/3/1 10:07:24   编辑:Linux編程

宏定義中 多行時用"\"換行。

  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. {
  3. fprintf(stderr, "%s:%d", __FILE__, __LINE__);
  4. system("pause");
  5. }

會在屏幕顯示當前文件的路徑和fprintf語句所在的行數。

Copyright © Linux教程網 All Rights Reserved