Sprintf () may cause an out-of-bounds problem. When releasing the memory, an error will be reported during the delete operation. Pay attention to this when using it.
For example:
First, create a character array and allocate memory to it.
Char * TMP = new char [255];
Memset (TMP, '/0', 255 );
Use sprintf () to copy char * Data to TMP. The data length exceeds 255:
Sprintf (TMP, "Data = % s", data );
The following error is generated during Delete [] TMP:
Char * TMP = new char [255]; <br/> file * fp = fopen (tmpfile, "R"); <br/> If (FP = NULL) {<br/> afxmessagebox ("Open File error"); <br/> return 0; <br/>}< br/> // 1. read the file content <br/> char * Data = new char [1024*10]; <br/> memset (data, '/0', 1024*10 ); <br/> int K = 0; <br/> int Total = 0; <br/> while (! Feof (FP) {<br/> K = fread (Data + total, FP); <br/> total + = K; <br/>}< br/> fclose (FP); <br/> sprintf (TMP, "Data = % s", data ); <br/> afxmessagebox (TMP); <br/> Delete [] TMP; <br/>