C/C++マニアック雑記5
をテンプレートにして作成
[
トップ
] [
新規
|
一覧
|
単語検索
|
最終更新
|
ヘルプ
]
開始行:
プログラムの汎用性は命なので
文字列とか入出力の
汎用性を持たせたい
でもここで問題となるのが文字コードの問題だ
ウィンドウズの関数などは文字コードを正しく使わないと
コンパイラはエラーを掃く
(めんどくせぇ・・・)
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、マルチバイト文字に対応するためにTCHA...
// _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); // ファイルへの出力スト...
Output(tofs); // tofs を渡して、ファイルに出力する。
return 0;
}
#vote((^ω^)ktkr[9],普通[4],。(`ω´#)。なんじゃこりゃ...
終了行:
プログラムの汎用性は命なので
文字列とか入出力の
汎用性を持たせたい
でもここで問題となるのが文字コードの問題だ
ウィンドウズの関数などは文字コードを正しく使わないと
コンパイラはエラーを掃く
(めんどくせぇ・・・)
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、マルチバイト文字に対応するためにTCHA...
// _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); // ファイルへの出力スト...
Output(tofs); // tofs を渡して、ファイルに出力する。
return 0;
}
#vote((^ω^)ktkr[9],普通[4],。(`ω´#)。なんじゃこりゃ...
ページ名: