|
プログラムの汎用性は命なので でもここで問題となるのが文字コードの問題だ VC++のコンパイラの設定でマルチコード文字か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;
}
|