1

Cstring::Format により、Visual Studio 2008 の vsprintf.c 行 244 で「バッファが小さすぎます」というデバッグ アサーションが発生します。

//inside the function.

somefile.Open (//open for mode read) //somefile is CFile.

char* buff = new [somefile.GetLength()];

somefile.Read ((void*)buff, somefile.GetLength());

CString cbuff;
cbuff.Format ("%s",buff); //this line causes the debug assertion.

//and so on 

CString::Format() が「バッファが小さすぎます」というエラーを引き起こす理由は何ですか? これは、常にデバッグ アサーション エラーになるとは限りません。

4

3 に答える 3

3

別の解決策は次のとおりです。

somefile.Open (//open for mode read) //somefile is CFile.
int buflen = somefile.GetLength();

CString cbuff;
somefile.Read ((void*)cbuff.GetBuffer(buflen), buflen);
cbuff.ReleaseBuffer();

中間変数ではなく、文字列バッファーに直接読み取ります。CString::GetBuffer() 関数は、"new char[]" を割り当てたときに忘れていた余分なバイトを文字列に自動的に追加します。

于 2012-08-10T05:09:55.763 に答える
0

文字列は '\0' で終わるため、バッファ サイズが十分ではありません

于 2012-08-10T02:26:03.807 に答える