當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


C++ fwide()用法及代碼示例

C++ 中的fwide() 函數或者嘗試設置方向,或者查詢給定文件流的當前方向。

fwide() 函數在<cwchar> 頭文件中定義。

fwide()原型

int fwide( FILE* stream, int mode );

根據 mode 的值,決定 fwide 函數的作用。

  • 如果 mode > 0 ,此函數嘗試製作流 wide-oriented。
  • 如果 mode < 0 ,此函數嘗試使流麵向字節。
  • 如果 mode == 0 ,此函數僅查詢流的當前方向。
  • 如果流的方向已經通過執行輸出或之前對 fwide 的調用確定,則此函數不執行任何操作。

參數:

  • stream:指向文件流的指針以設置或查詢方向。
  • mode :一個整數值,用於確定是設置還是查詢流的方向。

返回:

fwide() 函數返回:

  • 如果流是wide-oriented,則為正整數。
  • 如果流是麵向字節的,則為負整數。
  • 如果流沒有方向,則為零。

示例:fwide() 函數如何工作?

#include <cwchar>
#include <cstdio>
#include <iostream>
using namespace std;

int main()
{
    FILE *fp;
    int retVal;

    fp = fopen("file.txt","r");
    retVal = fwide(fp,0);
    if (retVal == 0)
        cout << "Stream has no orientation" << endl;
    else if (retVal > 0)
        cout << "Stream is wide-oriented" << endl;
    else
        cout << "Stream is byte-oriented" << endl;

    /* wide oriented stream */
    cout << "Setting stream to wide-orientation" << endl;
    retVal = fwide(fp,1);
    if (retVal == 0)
        cout << "Stream has no orientation" << endl;
    else if (retVal > 0)
        cout << "Stream is wide-oriented" << endl;
    else
        cout << "Stream is byte-oriented" << endl;

    return 0;
}

運行程序時,輸出將是:

Stream has no orientation
Setting stream to wide-orientation
Stream is wide-oriented

相關用法


注:本文由純淨天空篩選整理自 C++ fwide()。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。