名前空間
変種
操作

std::fwide

From cppreference.com
< cpp‎ | io‎ | c
 
 
 
C形式I/O
型とオブジェクト
関数
ファイルアクセス
fwide

直接入出力
非書式化入出力
書式付き入力
(C++11)(C++11)(C++11)    
(C++11)(C++11)(C++11)    
書式付き出力
ファイルポジショニング
エラーハンドリング
ファイル操作
 
ヘッダ <cwchar> で定義
int fwide( std::FILE* stream, int mode );

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

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

目次

[編集] パラメータ

stream - 変更または問い合わせ対象の C I/O ストリームへのポインタ
モード - ストリームをワイドにするにはゼロより大きい整数値、ストリームをナローにするにはゼロより小さい値、または問い合わせのみの場合はゼロ

[編集] 戻り値

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

[編集]

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

#include <cstdio>
#include <cstdlib>
#include <cwchar>
#include <iostream>
 
void show_orientation(int n)
{
    n < 0 ? std::wcout << "\tnarrow orientation\n" :
    n > 0 ? std::wcout << "\twide orientation\n" :
            std::wcout << "\tno orientation\n";
}
 
void try_read(FILE* fp)
{
    if (const int c = std::fgetc(fp); c == EOF)
        std::wcout << "\tnarrow character read failed\n";
    else
        std::wcout << "\tnarrow character read '" << static_cast<char>(c) << "'\n";
 
    if (const wint_t wc = std::fgetwc(fp); wc == WEOF)
        std::wcout << "\twide character read failed\n";
    else
        std::wcout << "\twide character read '" << static_cast<wchar_t>(wc) << "'\n";
}
 
int main()
{
    enum fwide_orientation : int { narrow = -1, query, wide };
 
    FILE* fp = std::fopen("main.cpp", "r");
    if (!fp)
    {
        std::wcerr << "fopen() failed\n";
        return EXIT_FAILURE;
    }
 
    std::wcout << "1) A newly opened stream has no orientation.\n";
    show_orientation(std::fwide(fp, fwide_orientation::query));
 
    std::wcout << "2) Establish byte orientation.\n";
    show_orientation(std::fwide(fp, fwide_orientation::narrow));
    try_read(fp);
 
    std::wcout << "3) Only freopen() can reset stream orientation.\n";
    if (std::freopen("main.cpp", "r", fp) == NULL)
    {
        std::wcerr << "freopen() failed\n";
        return EXIT_FAILURE;
    }
 
    std::wcout << "4) A reopened stream has no orientation.\n";
    show_orientation(std::fwide(fp, fwide_orientation::query));
 
    std::wcout << "5) Establish wide orientation.\n";
    show_orientation(std::fwide(fp, fwide_orientation::wide));
    try_read(fp);
 
    std::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 '#'

[編集] 関連項目

ファイルを開く
(関数) [編集]
English 日本語 中文(简体) 中文(繁體)