首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C语言 >

sprintf_s函数怪异的有关问题

2013-01-04 
sprintf_s函数怪异的问题PFILEINFO ppstor.GetHand()char buffer[MAX_PATH]{0}char temp[MAX_PATH]{

sprintf_s函数怪异的问题
PFILEINFO p;
p=stor.GetHand();
char buffer[MAX_PATH]={0};
char temp[MAX_PATH]={0};
int sum=0;
while(p->Next)
{
wcstombs(temp,p->FileName.GetBuffer(p->FileName.GetLength()),MAX_PATH);
sum+=sprintf_s(buffer+sum,MAX_PATH,"\n文件: %s delete!",temp);
p=p->Next;
}

本段代码,有什么不对的吗?为什么我在执行时发现,当循环进行第二次时,sum+=sprintf_s(buffer+sum,MAX_PATH,"\n文件: %s delete!",temp);执行完毕后,P指针的地址被更改了,为什么?
                                                         帮助解答下
[解决办法]
ms-help://MS.VSCC.v90/MS.MSDNQTR.v90.chs/dv_vccrt/html/424f0a29-22ef-40e8-b565-969f5f57782f.htm

 Collapse AllExpand All      Code: All Code: Multiple Code: Visual Basic Code: C# Code: Visual C++ Code: J# Code: JScript  
Visual Basic
C#
Visual C++
J#
JScript
Run-Time Library Reference 
sprintf_s, _sprintf_s_l, swprintf_s, _swprintf_s_l 
Example  See Also  Send Feedback 
 

Write formatted data to a string. These are versions of sprintf, _sprintf_l, swprintf, _swprintf_l, __swprintf_l with security enhancements as described in Security Enhancements in the CRT.

 
int sprintf_s(
   char *buffer,
   size_t sizeOfBuffer,
   const char *format [,
      argument] ... 
);
int _sprintf_s_l(
   char *buffer,
   size_t sizeOfBuffer,
   const char *format,
   locale_t locale [,
      argument] ... 
);
int swprintf_s(
   wchar_t *buffer,
   size_t sizeOfBuffer,
   const wchar_t *format [,
      argument]...
);
int _swprintf_s_l(
   wchar_t *buffer,
   size_t sizeOfBuffer,
   const wchar_t *format,
   locale_t locale [,
      argument]…
);
template <size_t size>
int sprintf_s(
   char (&buffer)[size],
   const char *format [,
      argument] ... 
); // C++ only
template <size_t size>
int swprintf_s(
   wchar_t (&buffer)[size],
   const wchar_t *format [,
      argument]...
); // C++ only
 

Parameters
buffer
Storage location for output

sizeOfBuffer
Maximum number of characters to store.

format
Format-control string

argument
Optional arguments

locale
The locale to use.

For more information, see Format Specifications.



Return Value
The number of characters written, or –1 if an error occurred. If buffer or format is a null pointer, sprintf_s and swprintf_s return -1 and set errno to EINVAL.

sprintf_s returns the number of bytes stored in buffer, not counting the terminating null character. swprintf_s returns the number of wide characters stored in buffer, not counting the terminating null wide character.

Remarks
The sprintf_s function formats and stores a series of characters and values in buffer. Each argument (if any) is converted and output according to the corresponding format specification in format. The format consists of ordinary characters and has the same form and function as the format argument for printf. A null character is appended after the last character written. If copying occurs between strings that overlap, the behavior is undefined.

One main difference between sprintf_s and sprintf is that sprintf_s checks the format string for valid formatting characters, whereas sprintf only checks if the format string or buffer are NULL pointers. If either check fails, the invalid parameter handler is invoked, as described in Parameter Validation. If execution is allowed to continue, the function returns -1 and sets errno to EINVAL.

The other main difference between sprintf_s and sprintf is that sprintf_s takes a length parameter specifying the size of the output buffer in characters. If the buffer is too small for the text being printed then the buffer is set to an empty string and the invalid parameter handler is invoked. Unlike snprintf, sprintf_s guarantees that the buffer will be null-terminated (unless the buffer size is zero).

swprintf_s is a wide-character version of sprintf_s; the pointer arguments to swprintf_s are wide-character strings. Detection of encoding errors in swprintf_s may differ from that in sprintf_s. The versions of these functions with the _l suffix are identical except that they use the locale parameter passed in instead of the current thread locale.

In C++, using these functions is simplified by template overloads; the overloads can infer buffer length automatically (eliminating the need to specify a size argument) and they can automatically replace older, non-secure functions with their newer, secure counterparts. For more information, see Secure Template Overloads.



There are versions of sprintf_s that offer additional control over what happens if the buffer is too small. For more information, see _snprintf_s, _snprintf_s_l, _snwprintf_s, _snwprintf_s_l.

TCHAR.H routine 
 _UNICODE & _MBCS not defined
 _MBCS defined
 _UNICODE defined
 
_stprintf_s
 sprintf_s
 sprintf_s
 swprintf_s
 
_stprintf_s_l
 _sprintf_s_l
 _sprintf_s_l
 _swprintf_s_l
 

Requirements
Routine
 Required header
 
sprintf_s, _sprintf_s_l
 <stdio.h>
 
swprintf_s, _swprintf_s_l
 <stdio.h> or <wchar.h>
 

For additional compatibility information, see Compatibility in the Introduction.

Example
  Copy Code 
// crt_sprintf_s.c
// This program uses sprintf_s to format various
// data and place them in the string named buffer.
//

#include <stdio.h>

int main( void )
{
   char  buffer[200], s[] = "computer", c = 'l';
   int   i = 35, j;
   float fp = 1.7320534f;

   // Format and print various data: 
   j  = sprintf_s( buffer, 200,     "   String:    %s\n", s );
   j += sprintf_s( buffer + j, 200 - j, "   Character: %c\n", c );
   j += sprintf_s( buffer + j, 200 - j, "   Integer:   %d\n", i );
   j += sprintf_s( buffer + j, 200 - j, "   Real:      %f\n", fp );

   printf_s( "Output:\n%s\ncharacter count = %d\n", buffer, j );
}
 
  Copy Code 
Output:
   String:    computer
   Character: l
   Integer:   35
   Real:      1.732053

character count = 79
 
  Copy Code 
// crt_swprintf_s.c
// wide character example
// also demonstrates swprintf_s returning error code
#include <stdio.h>

int main( void )
{
   wchar_t buf[100];
   int len = swprintf_s( buf, 100, L"%s", L"Hello world" );
   printf( "wrote %d characters\n", len );
   len = swprintf_s( buf, 100, L"%s", L"Hello\xffff world" );
   // swprintf_s fails because string contains WEOF (\xffff)
   printf( "wrote %d characters\n", len );
}
 
  Copy Code 
wrote 11 characters
wrote -1 characters
 

.NET Framework Equivalent


System::String::Format

See Also
Concepts
Stream I/O
fprintf, _fprintf_l, fwprintf, _fwprintf_l
printf, _printf_l, wprintf, _wprintf_l
scanf, _scanf_l, wscanf, _wscanf_l
sscanf, _sscanf_l, swscanf, _swscanf_l
vprintf Functions
Send feedback on this topic to Microsoft.
[解决办法]
估计是溢出了,你查看一下sum的值,看起是否超过MAX_PATH的大小
[解决办法]
补充一下,这个不是这么写的,
sprintf_s(buffer+sum,MAX_PATH,"\n文件: %s delete!",temp);
改成
sprintf_s(buffer+sum,MAX_PATH-sum,"\n文件: %s delete!",temp);

热点排行