私のC++ WinAPIアプリケーションでC文字列とstd::string
を少し簡単に組み合わせる機能を作ろうとしています。std :: stringsとC-Stringsを組み合わせるとバッファオーバーランが発生する
ので、代わりにこれを行う:
TCHAR res[MAX_PATH];
_stprintf(res, _T("In functionX(): error occured where the variable values are %d, %u, %s, %c"), myInt, myUnsignedInt, myStr.c_str(), myChar);
MessageBox(NULL, res, _T("Error Occurred"), MB_OK);
私は(私はどこでもTCHARの配列を宣言維持する必要はありませんので、それは少し楽異なる文字列型をマージすることができた)これを実行する必要があります:
をtstring res = concat(_T("In functionX(): error occured with the variable values %d, %u, %s, %c"), myInt, myUnsignedInt, myStr.c_str(), myChar);
MessageBox(NULL, (LPTSTR)res.c_str(), _T("Error Occurred"), MB_OK);
私の問題: My機能CONCAT();パラメータの中に2つ以上の変数を渡すとが失敗し、なぜその理由がわからないのですか?私は、Microsoft Visual C++用の関数を実行するときに発生する
// The following function call causes the error
tstring ou = concat(_T("In functionX(): Failed to create temp file - %s - %s\r\n"), (LPTSTR)tempFileRootDir.c_str(), tempFile);
tstring WinFile::concat(TCHAR* strFormat, TCHAR* format, ...)
{
// tstring is either a std::string or std::wstring depending on whether unicode is used
// Post: Wrapper function to easily merge C++ strings with C Strings
va_list arguments;
va_start(arguments, format);
TCHAR res[MAX_PATH];
_stprintf(res, strFormat, format);
return tstring(res);
}
エラーは次のとおりです。
バッファオーバーランが は、プログラムの内部状態が破損していたApplication.exeで発生しています。ブレークを押して プログラムをデバッグするか、プログラムの終了を続行します。
詳細については、「バッファオーバーランをデバッグする方法 問題」を参照してください。
なぜMAX_PATH文字を持つと宣言していますか?パスをコピーしていません。あなたは、MAX_PATH以上の文字を書くことを試みていると思います。 –
オプションとして、ATL :: CStringを使用できます。 Formatメソッドを持っています。 –
'STD'を大文字にする理由は何ですか?それは普通、性感染症を意味するのですか?小文字のC++標準ライブラリの名前空間は 'std'です。 – jalf