Namespaces
Variants

fwide

From cppreference.net
< c ‎ | io
定義先ヘッダ <wchar.h>
int fwide ( FILE * stream, int mode ) ;
(C95以降)

mode > 0 の場合、 stream をワイド指向に設定しようと試みます。 mode < 0 の場合、 stream をバイト指向に設定しようと試みます。 mode == 0 の場合、ストリームの現在の向きのみを問い合わせます。

ストリームの向きが既に決定されている場合(出力の実行または以前の fwide の呼び出しによって)、この関数は何もしません。

目次

パラメータ

stream - 変更または問い合わせを行うC I/Oストリームへのポインタ
mode - 0より大きい整数値でストリームをワイドに設定、0より小さい値でストリームをナローに設定、0の場合は問い合わせのみを行う

戻り値

この呼び出し後にストリームがワイド指向である場合はゼロより大きい整数、バイト指向である場合はゼロより小さい整数、方向性を持たない場合はゼロ。

以下のコードはストリームの向きを設定およびリセットします。

#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: TBD)
  • 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)

関連項目

ファイルを開く
(関数)
翻訳内容: - "opens a file" → "ファイルを開く" - "(function)" → "(関数)" - HTMLタグ、属性、
タグ内のテキストは翻訳せず保持
- C++固有の用語(fopen, fopen_s, fwide, C11, C++ documentation)は翻訳せず保持