- 追加された行はこの色です。
- 削除された行はこの色です。
プログラムの汎用性は命なので
文字列とか入出力の
汎用性を持たせたい
でもここで問題となるのが文字コードの問題だ
ウィンドウズの関数などは文字コードを正しく使わないと
コンパイラはエラーを掃く
(めんどくせぇ・・・)
VC++のコンパイラの設定でマルチコード文字かUnicode文字の指定ができる
中にはUnicode専用の関数などもたまーにあったりして
切り替え用のコードとか書くにしても非常にめんどくさい
ならば、最初から両方に対応できるコードを書くしかない
しかもできるだけ楽して
正直Unicode、マルチバイトとかを
意識するコードを書きたくないので
次のようなヘッダーを作ってみた
多分困ってる人も多いと思うのでご利用はご自由に
//---------------------Unicode.h-----------------------------------//
#ifndef _UNICODE_H_
#define _UNICODE_H_
#include <iostream>
#include <fstream>
#include <sstream>
#include <iomanip>
#include <tchar.h>
#include <string>
#include <locale.h>
// 文字列はUnicode、マルチバイト文字に対応するためにTCHAR型がお勧めです
// _T("文字列")マクロで囲ってやるとTCHAR文字列として扱えます
// 標準出力 _tprintf ,printf ,wprintf
// バッファに整形出力 _stprintf ,sprintf ,swprintf
// 文字列比較 _tcscmp ,strcmp ,lstrcmp
// 標準で欲しいレベルなので
// std名前空間として扱う
_STD_BEGIN
// tstring型を定義
typedef std::basic_string<TCHAR> tstring;
// tstream型の定義
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_
//---------------------------------------------------------------------//
あえてstd名前空間に定義しました
使い方は下のように使います。
#include "Unicode.h"
// ストリームを引数に渡すときは
void Output(std::tostream& tos) {
tos << "Hello World!" << std::endl;
}
int main() {
std::tcout << "テスト" << std::endl;
const TCHAR* filename = _T("TestOutput.txt");
Output(std::tcout); // システムが用意した標準出力 tcout に出力する。
std::tofstream tofs(filename); // ファイルへの出力ストリームオブジェクト tofs を作成する。
Output(tofs); // tofs を渡して、ファイルに出力する。
return 0;
}
#vote((^ω^)ktkr[7],普通[3],。(`ω´#)。なんじゃこりゃああ[5])
#vote((^ω^)ktkr[8],普通[3],。(`ω´#)。なんじゃこりゃああ[5])