Cstring :: Format在vsprintf.c第244行的visual studio 2008中導致調試斷言,其中「buffer too small」太小。CString :: Format()導致調試斷言
//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()導致「緩衝區太小」錯誤?這並不總是得到調試斷言錯誤。
是否意味着我必須做一些類似於buff.Format(「%s \ 0」,buff)?或者我應該在文件中讀取時將char *的大小增加1? – user1589128 2012-08-10 02:31:21
對不起,有一個新手問題。但是我們假設字符緩衝區長度爲100個字節。如果我只讀取60個字節,緩衝區的最後40個字節是什麼? – user1589128 2012-08-10 02:35:02
是的。最後的40個字節仍然存在。 我的意思是: char * buff = new [somefile.GetLength()+ 1];由於您將該文件作爲長字符串進行讀取,因此它應該有一個用於結尾的額外字節。 – silvesthu 2012-08-10 02:53:47