プログラムの汎用性は命なので
文字列とか入出力の
汎用性を持たせたい

でもここで問題となるのが文字コードの問題だ
ウィンドウズの関数などは文字コードを正しく使わないと
コンパイラはエラーを掃く
(めんどくせぇええええ)

VC++のコンパイラの設定でマルチコード文字かUnicode文字の指定ができる
中にはUnicode専用の関数などもたまーにあったりして
切り替え用のコードとか書くにしても非常にめんどくさい
ならば、最初から両方に対応できるコードを書くしかない
しかもできるだけ楽して

正直Unicode、マルチバイトとかを
意識するコードを書きたくないので
次のようなヘッダーを作ってみた
多分困ってる人も多いと思うのでご利用はご自由に

#ifndef _UNICODE_H_

#define _UNICODE_H_

#include <iostream>

#include <fstream>

#include <sstream>

#include <iomanip>

#include <tchar.h>

#include <string>

#include <locale.h>

_STD_BEGIN

typedef std::basic_string<TCHAR> tstring;

typedef std::basic_ostream<TCHAR> tostream; // 標準出力ストリーム
typedef std::basic_istream<TCHAR> tistream; // 標準入力ストリーム
typedef std::basic_ostringstream<TCHAR> tostringstream; // 文字列出力ストリーム(整形格納用バッファ)
typedef std::basic_istringstream<TCHAR> tistringstream; // 文字列入力ストリーム(整形取得用バッファ)
typedef std::basic_ifstream<TCHAR> tifstream; // ファイル入力ストリーム
typedef std::basic_ofstream<TCHAR> tofstream; // ファイル出力ストリーム
typedef std::basic_fstream<TCHAR> tfstream; // ファイルストリーム

#ifdef UNICODE

#define tcin wcin // 入力

#define tcout wcout // 出力

#define tcerr wcerr // エラー

#define tclog wclog // ログ

#else

#define tcin cin // 入力

#define tcout cout // 出力

#define tcerr cerr // エラー

#define tclog clog // ログ

#endif

_STD_END

#endif // _UNICODE_H_

使い方は

#include "Unicode.h"

void Output(std::tostream& tos) {
tos << "Hello World!" << std::endl;
}

int main() {
std::tcout << "テスト";

Output(std::tcout); // システムが用意した標準出力 tcout に出力する。
std::tofstream tofs(_T("TestOutput.txt")); // ファイルへの出力ストリームオブジェクト tofs を作成する。
Output(tofs); // tofs を渡して、ファイルに出力する。
return 0;
}


トップ   新規 一覧 単語検索 最終更新   ヘルプ   最終更新のRSS