名稱空間
變體
操作

fwide

來自 cppreference.com
< c‎ | io
 
 
檔案輸入/輸出
型別和物件
        
函式
檔案訪問
fwide
(C95)
非格式化輸入/輸出
(C95)(C95)
(C95)
(C95)(C95)
(C95)
(C95)

格式化輸入
 
在標頭檔案 <wchar.h> 中定義
int fwide( FILE* stream, int mode );
(自 C95 起)

如果 mode > 0,嘗試使 stream 寬字元定向。如果 mode < 0,嘗試使 stream 位元組定向。如果 mode == 0,則只查詢流的當前定向。

如果流的定向已經確定(透過執行輸出或先前呼叫 fwide),此函式不執行任何操作。

目錄

[編輯] 引數

stream - 指向要修改或查詢的 C I/O 流的指標
mode - 整數值,大於零表示將流設定為寬字元,小於零表示將流設定為位元組,或零表示僅查詢

[編輯] 返回值

如果在此呼叫後流是寬字元定向的,則返回一個大於零的整數;如果在此呼叫後流是位元組定向的,則返回一個小於零的整數;如果流沒有定向,則返回零。

[編輯] 示例

以下程式碼設定和重置流的定向。

#include <stdio.h>
#include <stdlib.h>
#include <wchar.h>
 
void show_orientation(int n)
{
    n < 0 ? puts("\tnarrow orientation"):
    n > 0 ? puts("\twide orientation"):
            puts("\tno orientation");
}
 
void try_read(FILE* fp)
{
    int c = fgetc(fp);
    c == EOF
        ? printf("\tnarrow character read failed\n")
        : printf("\tnarrow character read '%c'\n", c);
 
    wint_t wc = fgetwc(fp);
    wc == WEOF
        ? printf("\twide character read failed\n")
        : printf("\twide character read '%lc'\n", wc);
}
 
int main(void)
{
    enum fwide_orientation { narrow = -1, query, wide };
 
    FILE* fp = fopen("main.cpp", "r");
    if (!fp)
    {
        perror("fopen() failed");
        return EXIT_FAILURE;
    }
 
    puts("1) A newly opened stream has no orientation.");
    show_orientation(fwide(fp, query));
 
    puts("2) Establish byte orientation.");
    show_orientation(fwide(fp, narrow));
    try_read(fp);
 
    puts("3) Only freopen() can reset stream orientation.");
    if (freopen("main.cpp", "r", fp) == NULL)
    {
       perror("freopen() failed");
       return EXIT_FAILURE;
    }
 
    puts("4) A reopened stream has no orientation.");
    show_orientation(fwide(fp, query));
 
    puts("5) Establish wide orientation.");
    show_orientation(fwide(fp, wide));
    try_read(fp);
 
    fclose(fp);
}

可能的輸出

1) A newly opened stream has no orientation.
        no orientation
2) Establish byte orientation.
        narrow orientation
        narrow character read '#'
        wide character read failed
3) Only freopen() can reset stream orientation.
4) A reopened stream has no orientation.
        no orientation
5) Establish wide orientation.
        wide orientation
        narrow character read failed
        wide character read '#'

[編輯] 參考

  • C23 標準 (ISO/IEC 9899:2024)
  • 7.29.3.5 fwide 函式(p:待定)
  • C17 標準 (ISO/IEC 9899:2018)
  • 7.29.3.5 fwide 函式(p:309)
  • C11 標準 (ISO/IEC 9899:2011)
  • 7.29.3.5 fwide 函式(p:423)
  • C99 標準 (ISO/IEC 9899:1999)
  • 7.24.3.5 fwide 函式(p:369)

[編輯] 另請參閱

開啟檔案
(function) [編輯]