snprintf跟_snprintf的区别

来源:互联网 发布:阿里云企业邮箱免费吗 编辑:程序博客网 时间:2024/04/30 09:33



snprintf跟_snprintf的区别

snprintf函数并不是标准c/c++中规定的函数,但是在许多编译器中,厂商提供了其实现的版本。
在gcc中,该函数名称就snprintf,而在VC中称为_snprintf。
由于不是标准函数,没有一个统一的标准来规定该函数的行为,所以导致了各厂商间的实现版本可
能会有差异。今天也的的确确看到了差异,因为这个小小的差异是我的程序无法正常的处理数据。
这个小小的差异发生在count参数。在VC中,这个count就是要写入的总字符串字符数,例如:
//VC
int main(int argc, char * argv[])
...{
    char   buff[100];
     printf("%d ",_snprintf(buff,10,"1234567890ab"));
     printf("%s",buff);
    return 0;
}

//Linxu:gcc/g++
#include <stdio.h>
int main(int argc, char * argv[])
...{
    char   buff[100];
     printf("%d ",snprintf(buff,10,"1234567890ab"));
     printf("%s",buff);
    return 0;
}
vc程序的输出是:
-1
1234567890@
gcc程序的输出是:
12
123456789
从输出结果可以知道:VC中的_snprintf的count参数表示,会向buff中写入count个字符,不包括'\0'字符,
并且不会在字符串末尾添加'\0'符,并且,如果字符串超过count,函数返回-1以标志可能导致的错误;gcc
中的snprintf函数的count参数表示,向buff中写入10个字符,包括'\0'字符,并且,返回实际的字符串长度,
例子中为12。  如果不了解这些函数在各平台间的差异,也许我们的程序在移植过程中会变得很脆弱。我们应该小心各种各样
的陷阱。

下面是MSDN对_snprintf函数的解释:

int _snprintf( char *buffersize_t countconst char *format [, argument] ... ); Parameters
buffer
Storage location for output
count
Maximum number of characters to store
format
Format-control string
argument
Optional arguments
Libraries

All versions of the C run-time libraries.

Return Value

_snprintf returns the number of bytes stored in buffer, not counting the terminating null character. If the number of bytes required to store the data exceeds count, then count bytes of data are stored in buffer and a negative value is returned.

Remarks

The _snprintf function formats and stores count or fewer characters and values (including a terminating null character that is always appended unless count is zero or the formatted string length is greater than or equal to count characters) 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. If copying occurs between strings that overlap, the behavior is undefined.

snprintf跟_snprintf的区别

snprintf函数并不是标准c/c++中规定的函数,但是在许多编译器中,厂商提供了其实现的版本。
在gcc中,该函数名称就snprintf,而在VC中称为_snprintf。
由于不是标准函数,没有一个统一的标准来规定该函数的行为,所以导致了各厂商间的实现版本可
能会有差异。今天也的的确确看到了差异,因为这个小小的差异是我的程序无法正常的处理数据。
这个小小的差异发生在count参数。在VC中,这个count就是要写入的总字符串字符数,例如:
//VC
int main(int argc, char * argv[])
...{
    char   buff[100];
     printf("%d ",_snprintf(buff,10,"1234567890ab"));
     printf("%s",buff);
    return 0;
}

//Linxu:gcc/g++
#include <stdio.h>
int main(int argc, char * argv[])
...{
    char   buff[100];
     printf("%d ",snprintf(buff,10,"1234567890ab"));
     printf("%s",buff);
    return 0;
}
vc程序的输出是:
-1
1234567890@
gcc程序的输出是:
12
123456789
从输出结果可以知道:VC中的_snprintf的count参数表示,会向buff中写入count个字符,不包括'\0'字符,
并且不会在字符串末尾添加'\0'符,并且,如果字符串超过count,函数返回-1以标志可能导致的错误;gcc
中的snprintf函数的count参数表示,向buff中写入10个字符,包括'\0'字符,并且,返回实际的字符串长度,
例子中为12。  如果不了解这些函数在各平台间的差异,也许我们的程序在移植过程中会变得很脆弱。我们应该小心各种各样
的陷阱。

下面是MSDN对_snprintf函数的解释:

int _snprintf( char *buffersize_t countconst char *format [, argument] ... ); Parameters
buffer
Storage location for output
count
Maximum number of characters to store
format
Format-control string
argument
Optional arguments
Libraries

All versions of the C run-time libraries.

Return Value

_snprintf returns the number of bytes stored in buffer, not counting the terminating null character. If the number of bytes required to store the data exceeds count, then count bytes of data are stored in buffer and a negative value is returned.

Remarks

The _snprintf function formats and stores count or fewer characters and values (including a terminating null character that is always appended unless count is zero or the formatted string length is greater than or equal to count characters) 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. If copying occurs between strings that overlap, the behavior is undefined.


snprintf函数并不是标准c/c++中规定的函数,但是在许多编译器中,厂商提供了其实现的版本。
在gcc中,该函数名称就snprintf,而在VC中称为_snprintf。
由于不是标准函数,没有一个统一的标准来规定该函数的行为,所以导致了各厂商间的实现版本可
能会有差异。今天也的的确确看到了差异,因为这个小小的差异是我的程序无法正常的处理数据。
这个小小的差异发生在count参数。在VC中,这个count就是要写入的总字符串字符数,例如:
//VC

int main(int argc, char * argv[])
...{
    char   buff[100];
     printf("%d ",_snprintf(buff,10,"1234567890ab"));
     printf("%s",buff);
    return 0;
}


//Linxu:gcc/g++

#include <stdio.h>
int main(int argc, char * argv[])
...{
    char   buff[100];
     printf("%d ",snprintf(buff,10,"1234567890ab"));
     printf("%s",buff);
    return 0;
}
vc程序的输出是:
-1
1234567890@
gcc程序的输出是:
12
123456789
从输出结果可以知道:VC中的_snprintf的count参数表示,会向buff中写入count个字符,不包括'\0'字符,
并且不会在字符串末尾添加'\0'符,并且,如果字符串超过count,函数返回-1以标志可能导致的错误;gcc
中的snprintf函数的count参数表示,向buff中写入10个字符,包括'\0'字符,并且,返回实际的字符串长度,
例子中为12。  如果不了解这些函数在各平台间的差异,也许我们的程序在移植过程中会变得很脆弱。我们应该小心各种各样
的陷阱。

下面是MSDN对_snprintf函数的解释:

int _snprintf( char *buffersize_t countconst char *format [, argument] ... ); Parameters
buffer
Storage location for output
count
Maximum number of characters to store
format
Format-control string
argument
Optional arguments
Libraries

All versions of the C run-time libraries.

Return Value

_snprintf returns the number of bytes stored in buffer, not counting the terminating null character. If the number of bytes required to store the data exceeds count, then count bytes of data are stored in buffer and a negative value is returned.

Remarks

The _snprintf function formats and stores count or fewer characters and values (including a terminating null character that is always appended unless count is zero or the formatted string length is greater than or equal to count characters) 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. If copying occurs between strings that overlap, the behavior is undefined.



snprintf跟_snprintf的区别

snprintf函数并不是标准c/c++中规定的函数,但是在许多编译器中,厂商提供了其实现的版本。
在gcc中,该函数名称就snprintf,而在VC中称为_snprintf。
由于不是标准函数,没有一个统一的标准来规定该函数的行为,所以导致了各厂商间的实现版本可
能会有差异。今天也的的确确看到了差异,因为这个小小的差异是我的程序无法正常的处理数据。
这个小小的差异发生在count参数。在VC中,这个count就是要写入的总字符串字符数,例如:
//VC
int main(int argc, char * argv[])
...{
    char   buff[100];
     printf("%d ",_snprintf(buff,10,"1234567890ab"));
     printf("%s",buff);
    return 0;
}

//Linxu:gcc/g++
#include <stdio.h>
int main(int argc, char * argv[])
...{
    char   buff[100];
     printf("%d ",snprintf(buff,10,"1234567890ab"));
     printf("%s",buff);
    return 0;
}
vc程序的输出是:
-1
1234567890@
gcc程序的输出是:
12
123456789
从输出结果可以知道:VC中的_snprintf的count参数表示,会向buff中写入count个字符,不包括'\0'字符,
并且不会在字符串末尾添加'\0'符,并且,如果字符串超过count,函数返回-1以标志可能导致的错误;gcc
中的snprintf函数的count参数表示,向buff中写入10个字符,包括'\0'字符,并且,返回实际的字符串长度,
例子中为12。  如果不了解这些函数在各平台间的差异,也许我们的程序在移植过程中会变得很脆弱。我们应该小心各种各样
的陷阱。

下面是MSDN对_snprintf函数的解释:

int _snprintf( char *buffersize_t countconst char *format [, argument] ... ); Parameters
buffer
Storage location for output
count
Maximum number of characters to store
format
Format-control string
argument
Optional arguments
Libraries

All versions of the C run-time libraries.

Return Value

_snprintf returns the number of bytes stored in buffer, not counting the terminating null character. If the number of bytes required to store the data exceeds count, then count bytes of data are stored in buffer and a negative value is returned.

Remarks

The _snprintf function formats and stores count or fewer characters and values (including a terminating null character that is always appended unless count is zero or the formatted string length is greater than or equal to count characters) 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. If copying occurs between strings that overlap, the behavior is undefined.

snprintf跟_snprintf的区别

snprintf函数并不是标准c/c++中规定的函数,但是在许多编译器中,厂商提供了其实现的版本。
在gcc中,该函数名称就snprintf,而在VC中称为_snprintf。
由于不是标准函数,没有一个统一的标准来规定该函数的行为,所以导致了各厂商间的实现版本可
能会有差异。今天也的的确确看到了差异,因为这个小小的差异是我的程序无法正常的处理数据。
这个小小的差异发生在count参数。在VC中,这个count就是要写入的总字符串字符数,例如:
//VC
int main(int argc, char * argv[])
...{
    char   buff[100];
     printf("%d ",_snprintf(buff,10,"1234567890ab"));
     printf("%s",buff);
    return 0;
}

//Linxu:gcc/g++
#include <stdio.h>
int main(int argc, char * argv[])
...{
    char   buff[100];
     printf("%d ",snprintf(buff,10,"1234567890ab"));
     printf("%s",buff);
    return 0;
}
vc程序的输出是:
-1
1234567890@
gcc程序的输出是:
12
123456789
从输出结果可以知道:VC中的_snprintf的count参数表示,会向buff中写入count个字符,不包括'\0'字符,
并且不会在字符串末尾添加'\0'符,并且,如果字符串超过count,函数返回-1以标志可能导致的错误;gcc
中的snprintf函数的count参数表示,向buff中写入10个字符,包括'\0'字符,并且,返回实际的字符串长度,
例子中为12。  如果不了解这些函数在各平台间的差异,也许我们的程序在移植过程中会变得很脆弱。我们应该小心各种各样
的陷阱。

下面是MSDN对_snprintf函数的解释:

int _snprintf( char *buffersize_t countconst char *format [, argument] ... ); Parameters
buffer
Storage location for output
count
Maximum number of characters to store
format
Format-control string
argument
Optional arguments
Libraries

All versions of the C run-time libraries.

Return Value

_snprintf returns the number of bytes stored in buffer, not counting the terminating null character. If the number of bytes required to store the data exceeds count, then count bytes of data are stored in buffer and a negative value is returned.

Remarks

The _snprintf function formats and stores count or fewer characters and values (including a terminating null character that is always appended unless count is zero or the formatted string length is greater than or equal to count characters) 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. If copying occurs between strings that overlap, the behavior is undefined.


snprintf函数并不是标准c/c++中规定的函数,但是在许多编译器中,厂商提供了其实现的版本。
在gcc中,该函数名称就snprintf,而在VC中称为_snprintf。
由于不是标准函数,没有一个统一的标准来规定该函数的行为,所以导致了各厂商间的实现版本可
能会有差异。今天也的的确确看到了差异,因为这个小小的差异是我的程序无法正常的处理数据。
这个小小的差异发生在count参数。在VC中,这个count就是要写入的总字符串字符数,例如:
//VC

int main(int argc, char * argv[])
...{
    char   buff[100];
     printf("%d ",_snprintf(buff,10,"1234567890ab"));
     printf("%s",buff);
    return 0;
}


//Linxu:gcc/g++

#include <stdio.h>
int main(int argc, char * argv[])
...{
    char   buff[100];
     printf("%d ",snprintf(buff,10,"1234567890ab"));
     printf("%s",buff);
    return 0;
}
vc程序的输出是:
-1
1234567890@
gcc程序的输出是:
12
123456789
从输出结果可以知道:VC中的_snprintf的count参数表示,会向buff中写入count个字符,不包括'\0'字符,
并且不会在字符串末尾添加'\0'符,并且,如果字符串超过count,函数返回-1以标志可能导致的错误;gcc
中的snprintf函数的count参数表示,向buff中写入10个字符,包括'\0'字符,并且,返回实际的字符串长度,
例子中为12。  如果不了解这些函数在各平台间的差异,也许我们的程序在移植过程中会变得很脆弱。我们应该小心各种各样
的陷阱。

下面是MSDN对_snprintf函数的解释:

int _snprintf( char *buffersize_t countconst char *format [, argument] ... ); Parameters
buffer
Storage location for output
count
Maximum number of characters to store
format
Format-control string
argument
Optional arguments
Libraries

All versions of the C run-time libraries.

Return Value

_snprintf returns the number of bytes stored in buffer, not counting the terminating null character. If the number of bytes required to store the data exceeds count, then count bytes of data are stored in buffer and a negative value is returned.

Remarks

The _snprintf function formats and stores count or fewer characters and values (including a terminating null character that is always appended unless count is zero or the formatted string length is greater than or equal to count characters) 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. If copying occurs between strings that overlap, the behavior is undefined.



snprintf跟_snprintf的区别

snprintf函数并不是标准c/c++中规定的函数,但是在许多编译器中,厂商提供了其实现的版本。
在gcc中,该函数名称就snprintf,而在VC中称为_snprintf。
由于不是标准函数,没有一个统一的标准来规定该函数的行为,所以导致了各厂商间的实现版本可
能会有差异。今天也的的确确看到了差异,因为这个小小的差异是我的程序无法正常的处理数据。
这个小小的差异发生在count参数。在VC中,这个count就是要写入的总字符串字符数,例如:
//VC
int main(int argc, char * argv[])
...{
    char   buff[100];
     printf("%d ",_snprintf(buff,10,"1234567890ab"));
     printf("%s",buff);
    return 0;
}

//Linxu:gcc/g++
#include <stdio.h>
int main(int argc, char * argv[])
...{
    char   buff[100];
     printf("%d ",snprintf(buff,10,"1234567890ab"));
     printf("%s",buff);
    return 0;
}
vc程序的输出是:
-1
1234567890@
gcc程序的输出是:
12
123456789
从输出结果可以知道:VC中的_snprintf的count参数表示,会向buff中写入count个字符,不包括'\0'字符,
并且不会在字符串末尾添加'\0'符,并且,如果字符串超过count,函数返回-1以标志可能导致的错误;gcc
中的snprintf函数的count参数表示,向buff中写入10个字符,包括'\0'字符,并且,返回实际的字符串长度,
例子中为12。  如果不了解这些函数在各平台间的差异,也许我们的程序在移植过程中会变得很脆弱。我们应该小心各种各样
的陷阱。

下面是MSDN对_snprintf函数的解释:

int _snprintf( char *buffersize_t countconst char *format [, argument] ... ); Parameters
buffer
Storage location for output
count
Maximum number of characters to store
format
Format-control string
argument
Optional arguments
Libraries

All versions of the C run-time libraries.

Return Value

_snprintf returns the number of bytes stored in buffer, not counting the terminating null character. If the number of bytes required to store the data exceeds count, then count bytes of data are stored in buffer and a negative value is returned.

Remarks

The _snprintf function formats and stores count or fewer characters and values (including a terminating null character that is always appended unless count is zero or the formatted string length is greater than or equal to count characters) 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. If copying occurs between strings that overlap, the behavior is undefined.

snprintf跟_snprintf的区别

snprintf函数并不是标准c/c++中规定的函数,但是在许多编译器中,厂商提供了其实现的版本。
在gcc中,该函数名称就snprintf,而在VC中称为_snprintf。
由于不是标准函数,没有一个统一的标准来规定该函数的行为,所以导致了各厂商间的实现版本可
能会有差异。今天也的的确确看到了差异,因为这个小小的差异是我的程序无法正常的处理数据。
这个小小的差异发生在count参数。在VC中,这个count就是要写入的总字符串字符数,例如:
//VC
int main(int argc, char * argv[])
...{
    char   buff[100];
     printf("%d ",_snprintf(buff,10,"1234567890ab"));
     printf("%s",buff);
    return 0;
}

//Linxu:gcc/g++
#include <stdio.h>
int main(int argc, char * argv[])
...{
    char   buff[100];
     printf("%d ",snprintf(buff,10,"1234567890ab"));
     printf("%s",buff);
    return 0;
}
vc程序的输出是:
-1
1234567890@
gcc程序的输出是:
12
123456789
从输出结果可以知道:VC中的_snprintf的count参数表示,会向buff中写入count个字符,不包括'\0'字符,
并且不会在字符串末尾添加'\0'符,并且,如果字符串超过count,函数返回-1以标志可能导致的错误;gcc
中的snprintf函数的count参数表示,向buff中写入10个字符,包括'\0'字符,并且,返回实际的字符串长度,
例子中为12。  如果不了解这些函数在各平台间的差异,也许我们的程序在移植过程中会变得很脆弱。我们应该小心各种各样
的陷阱。

下面是MSDN对_snprintf函数的解释:

int _snprintf( char *buffersize_t countconst char *format [, argument] ... ); Parameters
buffer
Storage location for output
count
Maximum number of characters to store
format
Format-control string
argument
Optional arguments
Libraries

All versions of the C run-time libraries.

Return Value

_snprintf returns the number of bytes stored in buffer, not counting the terminating null character. If the number of bytes required to store the data exceeds count, then count bytes of data are stored in buffer and a negative value is returned.

Remarks

The _snprintf function formats and stores count or fewer characters and values (including a terminating null character that is always appended unless count is zero or the formatted string length is greater than or equal to count characters) 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. If copying occurs between strings that overlap, the behavior is undefined.


snprintf函数并不是标准c/c++中规定的函数,但是在许多编译器中,厂商提供了其实现的版本。
在gcc中,该函数名称就snprintf,而在VC中称为_snprintf。
由于不是标准函数,没有一个统一的标准来规定该函数的行为,所以导致了各厂商间的实现版本可
能会有差异。今天也的的确确看到了差异,因为这个小小的差异是我的程序无法正常的处理数据。
这个小小的差异发生在count参数。在VC中,这个count就是要写入的总字符串字符数,例如:
//VC

int main(int argc, char * argv[])
...{
    char   buff[100];
     printf("%d ",_snprintf(buff,10,"1234567890ab"));
     printf("%s",buff);
    return 0;
}


//Linxu:gcc/g++

#include <stdio.h>
int main(int argc, char * argv[])
...{
    char   buff[100];
     printf("%d ",snprintf(buff,10,"1234567890ab"));
     printf("%s",buff);
    return 0;
}
vc程序的输出是:
-1
1234567890@
gcc程序的输出是:
12
123456789
从输出结果可以知道:VC中的_snprintf的count参数表示,会向buff中写入count个字符,不包括'\0'字符,
并且不会在字符串末尾添加'\0'符,并且,如果字符串超过count,函数返回-1以标志可能导致的错误;gcc
中的snprintf函数的count参数表示,向buff中写入10个字符,包括'\0'字符,并且,返回实际的字符串长度,
例子中为12。  如果不了解这些函数在各平台间的差异,也许我们的程序在移植过程中会变得很脆弱。我们应该小心各种各样
的陷阱。

下面是MSDN对_snprintf函数的解释:

int _snprintf( char *buffersize_t countconst char *format [, argument] ... ); Parameters
buffer
Storage location for output
count
Maximum number of characters to store
format
Format-control string
argument
Optional arguments
Libraries

All versions of the C run-time libraries.

Return Value

_snprintf returns the number of bytes stored in buffer, not counting the terminating null character. If the number of bytes required to store the data exceeds count, then count bytes of data are stored in buffer and a negative value is returned.

Remarks

The _snprintf function formats and stores count or fewer characters and values (including a terminating null character that is always appended unless count is zero or the formatted string length is greater than or equal to count characters) 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. If copying occurs between strings that overlap, the behavior is undefined.



snprintf跟_snprintf的区别

snprintf函数并不是标准c/c++中规定的函数,但是在许多编译器中,厂商提供了其实现的版本。
在gcc中,该函数名称就snprintf,而在VC中称为_snprintf。
由于不是标准函数,没有一个统一的标准来规定该函数的行为,所以导致了各厂商间的实现版本可
能会有差异。今天也的的确确看到了差异,因为这个小小的差异是我的程序无法正常的处理数据。
这个小小的差异发生在count参数。在VC中,这个count就是要写入的总字符串字符数,例如:
//VC
int main(int argc, char * argv[])
...{
    char   buff[100];
     printf("%d ",_snprintf(buff,10,"1234567890ab"));
     printf("%s",buff);
    return 0;
}

//Linxu:gcc/g++
#include <stdio.h>
int main(int argc, char * argv[])
...{
    char   buff[100];
     printf("%d ",snprintf(buff,10,"1234567890ab"));
     printf("%s",buff);
    return 0;
}
vc程序的输出是:
-1
1234567890@
gcc程序的输出是:
12
123456789
从输出结果可以知道:VC中的_snprintf的count参数表示,会向buff中写入count个字符,不包括'\0'字符,
并且不会在字符串末尾添加'\0'符,并且,如果字符串超过count,函数返回-1以标志可能导致的错误;gcc
中的snprintf函数的count参数表示,向buff中写入10个字符,包括'\0'字符,并且,返回实际的字符串长度,
例子中为12。  如果不了解这些函数在各平台间的差异,也许我们的程序在移植过程中会变得很脆弱。我们应该小心各种各样
的陷阱。

下面是MSDN对_snprintf函数的解释:

int _snprintf( char *buffersize_t countconst char *format [, argument] ... ); Parameters
buffer
Storage location for output
count
Maximum number of characters to store
format
Format-control string
argument
Optional arguments
Libraries

All versions of the C run-time libraries.

Return Value

_snprintf returns the number of bytes stored in buffer, not counting the terminating null character. If the number of bytes required to store the data exceeds count, then count bytes of data are stored in buffer and a negative value is returned.

Remarks

The _snprintf function formats and stores count or fewer characters and values (including a terminating null character that is always appended unless count is zero or the formatted string length is greater than or equal to count characters) 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. If copying occurs between strings that overlap, the behavior is undefined.

snprintf跟_snprintf的区别

snprintf函数并不是标准c/c++中规定的函数,但是在许多编译器中,厂商提供了其实现的版本。
在gcc中,该函数名称就snprintf,而在VC中称为_snprintf。
由于不是标准函数,没有一个统一的标准来规定该函数的行为,所以导致了各厂商间的实现版本可
能会有差异。今天也的的确确看到了差异,因为这个小小的差异是我的程序无法正常的处理数据。
这个小小的差异发生在count参数。在VC中,这个count就是要写入的总字符串字符数,例如:
//VC
int main(int argc, char * argv[])
...{
    char   buff[100];
     printf("%d ",_snprintf(buff,10,"1234567890ab"));
     printf("%s",buff);
    return 0;
}

//Linxu:gcc/g++
#include <stdio.h>
int main(int argc, char * argv[])
...{
    char   buff[100];
     printf("%d ",snprintf(buff,10,"1234567890ab"));
     printf("%s",buff);
    return 0;
}
vc程序的输出是:
-1
1234567890@
gcc程序的输出是:
12
123456789
从输出结果可以知道:VC中的_snprintf的count参数表示,会向buff中写入count个字符,不包括'\0'字符,
并且不会在字符串末尾添加'\0'符,并且,如果字符串超过count,函数返回-1以标志可能导致的错误;gcc
中的snprintf函数的count参数表示,向buff中写入10个字符,包括'\0'字符,并且,返回实际的字符串长度,
例子中为12。  如果不了解这些函数在各平台间的差异,也许我们的程序在移植过程中会变得很脆弱。我们应该小心各种各样
的陷阱。

下面是MSDN对_snprintf函数的解释:

int _snprintf( char *buffersize_t countconst char *format [, argument] ... ); Parameters
buffer
Storage location for output
count
Maximum number of characters to store
format
Format-control string
argument
Optional arguments
Libraries

All versions of the C run-time libraries.

Return Value

_snprintf returns the number of bytes stored in buffer, not counting the terminating null character. If the number of bytes required to store the data exceeds count, then count bytes of data are stored in buffer and a negative value is returned.

Remarks

The _snprintf function formats and stores count or fewer characters and values (including a terminating null character that is always appended unless count is zero or the formatted string length is greater than or equal to count characters) 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. If copying occurs between strings that overlap, the behavior is undefined.


MSDN页面分别如下:

spirntf_s:

http://msdn.microsoft.com/zh-cn/library/ce3zzk1k%28VS.80%29.aspx

_snprintf:

http://msdn.microsoft.com/zh-cn/library/2ts7cx93%28v=VS.90%29.aspx

_snprintf_s:

http://msdn.microsoft.com/zh-cn/library/f30dzcf6.aspx

三个页面都有自己的例子。。其中后2个的例子比较多内容一些。

 

为免将来页面失效:

int sprintf_s(   char *buffer,   size_t sizeOfBuffer,   const char *format [,      argument] ... 

);

template <size_t size>int sprintf_s(   char (&buffer)[size],   const char *format [,      argument] ... 

); // C++ only

int _snprintf(   char *buffer,   size_t count,   const char *format [,      argument] ... 

);

int _snprintf(   char (&buffer)[size],   size_t count,   const char *format [,      argument] ... 

); // C++ only

int _snprintf_s(   char *buffer,   size_t sizeOfBuffer,   size_t count,   const char *format [,   argument] ... 

);

int _snprintf_s(   char (&buffer)[size],   size_t count,   const char *format [,   argument] ... 

); // C++ only

 

呼呼。。内容还挺多。

 

 

这里比较引人注目的是,_snprintf_s为什么在sizeOfBuffer的基础上,还要多加一个count?

count似乎是用来控制理想的宽度的。

如果得到的字符串超过了count,于是会被截断到count的长度后面再加一个null-teminate

当然,更高优先级的应该是sizeOfBuffer,必须不超过这个大小。这个就说到点子上了。

 

如果应该输出的字符串的大小已经达到了sizeOfBuffer,那么就溢出了。溢出的情况下,sprintf_s函数把这当做一个错误,会把buffer缓冲区置为一个空字符串""。

而_snprintf_s的好处就是,有了count参数,输出的字符串就算超过缓冲区长度,仍然会有输出,输出字符串被截断到count大小,在这个大小的字符串后面加null-teminate。

当然,如果count被设置成和sizeOfBuffer同样大,或者不合理的更大,那么这个count参数就失去了意义。

这时候,如果输出字符串将要达到或者超过sizeOfBuffer,一样导致一个错误,输出缓冲区被置为空字符串。

因此,如果希望缓冲区被尽量利用,可以把count参数置为_TRUNCATE,这样的情况下,实际上效果相当于是将count设置为sizeOfBuffer - 1。

 

 

至于C语言环境下,sprintf_s与_snprintf的对比:

注意到,_snprintf的参数用的是count,而sprintf_s的参数用的是sizeOfBuffer。这很能说明问题。

看下对_snprintf的说明:

Let len be the length of the formatted data string (not including the terminating null). len and count are in bytes for _snprintf, wide characters for _snwprintf.

If len < count, then len characters are stored in buffer, a null-terminator is appended, and len is returned.

If len = count, then len characters are stored in buffer, no null-terminator is appended, and len is returned.

If len > count, then count characters are stored in buffer, no null-terminator is appended, and a negative value is returned.

 

也就是说,_snprintf的count参数明明白白的就是一个count。

如果输出字符串刚好达到count,由于期待的最大长度就是count,那么输出字符串肯定要完整,不能截断。

但是假如字符串缓冲区的大小其实就是count,这怎么办?MS VCRT的设计者认为,在这种情况下应该把输出字符串的长度告知调用者,让调用者来决定是否自己添加null-teminate。

换句话说,调用_snprintf时要注意了,必须检查_snprintf的返回值,如果返回值不是正数,那么还得注意你的字符串缓冲区并不是null-teminate结尾的。

 

总结来说,sprintf_s在缓冲区不够大时会失败,失败时缓冲区中是一个空字符串。

_snprintf不会失败,但是必须注意如果缓冲区不够大,缓冲区的内容将不是null-teminate的,必须自己注意字符串的结束。

_snprintf_s结合了2者的优点,只要count参数设置合理,函数就不会因缓冲区不够而失败。

 

但是观察_snprintf_s的说明,有一个很有趣的内容。

这3族函数中,有失败情况的2个函数sprintf_s和_snprintf_s中,(再次强调,我这里的失败的意思是,调用后缓冲区里是一个空字符串),_set_invalid_parameter_handler设置的错误处理器,在失败的情况下会被调用。

 

而截断的情况下,错误处理器并不会被调用。

 

VC的库开发者总是提供一些怪怪的东西。无论如何,让代码更加安全总是符合大家的总体期望的。

另外补充一下,查阅这些字符串安全函数的资料的时候要注意,

对微软来说,凡是限制字符串复制长度的函数,这些设计者仍然认为是不安全的,因为逻辑上来说,

这些长度参数只是限制了源字符串被复制的长度,而不是目标缓冲区的长度。

也就是说,微软的这些设计者认为,安全的方式其实是依赖C++的机制,辨认出目标缓冲区的真正大小,以此实现安全的复制。




snprintf跟_snprintf的区别

snprintf函数并不是标准c/c++中规定的函数,但是在许多编译器中,厂商提供了其实现的版本。
在gcc中,该函数名称就snprintf,而在VC中称为_snprintf。
由于不是标准函数,没有一个统一的标准来规定该函数的行为,所以导致了各厂商间的实现版本可
能会有差异。今天也的的确确看到了差异,因为这个小小的差异是我的程序无法正常的处理数据。
这个小小的差异发生在count参数。在VC中,这个count就是要写入的总字符串字符数,例如:
//VC
int main(int argc, char * argv[])
...{
    char   buff[100];
     printf("%d ",_snprintf(buff,10,"1234567890ab"));
     printf("%s",buff);
    return 0;
}

//Linxu:gcc/g++
#include <stdio.h>
int main(int argc, char * argv[])
...{
    char   buff[100];
     printf("%d ",snprintf(buff,10,"1234567890ab"));
     printf("%s",buff);
    return 0;
}
vc程序的输出是:
-1
1234567890@
gcc程序的输出是:
12
123456789
从输出结果可以知道:VC中的_snprintf的count参数表示,会向buff中写入count个字符,不包括'\0'字符,
并且不会在字符串末尾添加'\0'符,并且,如果字符串超过count,函数返回-1以标志可能导致的错误;gcc
中的snprintf函数的count参数表示,向buff中写入10个字符,包括'\0'字符,并且,返回实际的字符串长度,
例子中为12。  如果不了解这些函数在各平台间的差异,也许我们的程序在移植过程中会变得很脆弱。我们应该小心各种各样
的陷阱。

下面是MSDN对_snprintf函数的解释:

int _snprintf( char *buffersize_t countconst char *format [, argument] ... ); Parameters
buffer
Storage location for output
count
Maximum number of characters to store
format
Format-control string
argument
Optional arguments
Libraries

All versions of the C run-time libraries.

Return Value

_snprintf returns the number of bytes stored in buffer, not counting the terminating null character. If the number of bytes required to store the data exceeds count, then count bytes of data are stored in buffer and a negative value is returned.

Remarks

The _snprintf function formats and stores count or fewer characters and values (including a terminating null character that is always appended unless count is zero or the formatted string length is greater than or equal to count characters) 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. If copying occurs between strings that overlap, the behavior is undefined.

snprintf跟_snprintf的区别

snprintf函数并不是标准c/c++中规定的函数,但是在许多编译器中,厂商提供了其实现的版本。
在gcc中,该函数名称就snprintf,而在VC中称为_snprintf。
由于不是标准函数,没有一个统一的标准来规定该函数的行为,所以导致了各厂商间的实现版本可
能会有差异。今天也的的确确看到了差异,因为这个小小的差异是我的程序无法正常的处理数据。
这个小小的差异发生在count参数。在VC中,这个count就是要写入的总字符串字符数,例如:
//VC
int main(int argc, char * argv[])
...{
    char   buff[100];
     printf("%d ",_snprintf(buff,10,"1234567890ab"));
     printf("%s",buff);
    return 0;
}

//Linxu:gcc/g++
#include <stdio.h>
int main(int argc, char * argv[])
...{
    char   buff[100];
     printf("%d ",snprintf(buff,10,"1234567890ab"));
     printf("%s",buff);
    return 0;
}
vc程序的输出是:
-1
1234567890@
gcc程序的输出是:
12
123456789
从输出结果可以知道:VC中的_snprintf的count参数表示,会向buff中写入count个字符,不包括'\0'字符,
并且不会在字符串末尾添加'\0'符,并且,如果字符串超过count,函数返回-1以标志可能导致的错误;gcc
中的snprintf函数的count参数表示,向buff中写入10个字符,包括'\0'字符,并且,返回实际的字符串长度,
例子中为12。  如果不了解这些函数在各平台间的差异,也许我们的程序在移植过程中会变得很脆弱。我们应该小心各种各样
的陷阱。

下面是MSDN对_snprintf函数的解释:

int _snprintf( char *buffersize_t countconst char *format [, argument] ... ); Parameters
buffer
Storage location for output
count
Maximum number of characters to store
format
Format-control string
argument
Optional arguments
Libraries

All versions of the C run-time libraries.

Return Value

_snprintf returns the number of bytes stored in buffer, not counting the terminating null character. If the number of bytes required to store the data exceeds count, then count bytes of data are stored in buffer and a negative value is returned.

Remarks

The _snprintf function formats and stores count or fewer characters and values (including a terminating null character that is always appended unless count is zero or the formatted string length is greater than or equal to count characters) 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. If copying occurs between strings that overlap, the behavior is undefined.


snprintf函数并不是标准c/c++中规定的函数,但是在许多编译器中,厂商提供了其实现的版本。
在gcc中,该函数名称就snprintf,而在VC中称为_snprintf。
由于不是标准函数,没有一个统一的标准来规定该函数的行为,所以导致了各厂商间的实现版本可
能会有差异。今天也的的确确看到了差异,因为这个小小的差异是我的程序无法正常的处理数据。
这个小小的差异发生在count参数。在VC中,这个count就是要写入的总字符串字符数,例如:
//VC

int main(int argc, char * argv[])
...{
    char   buff[100];
     printf("%d ",_snprintf(buff,10,"1234567890ab"));
     printf("%s",buff);
    return 0;
}


//Linxu:gcc/g++

#include <stdio.h>
int main(int argc, char * argv[])
...{
    char   buff[100];
     printf("%d ",snprintf(buff,10,"1234567890ab"));
     printf("%s",buff);
    return 0;
}
vc程序的输出是:
-1
1234567890@
gcc程序的输出是:
12
123456789
从输出结果可以知道:VC中的_snprintf的count参数表示,会向buff中写入count个字符,不包括'\0'字符,
并且不会在字符串末尾添加'\0'符,并且,如果字符串超过count,函数返回-1以标志可能导致的错误;gcc
中的snprintf函数的count参数表示,向buff中写入10个字符,包括'\0'字符,并且,返回实际的字符串长度,
例子中为12。  如果不了解这些函数在各平台间的差异,也许我们的程序在移植过程中会变得很脆弱。我们应该小心各种各样
的陷阱。

下面是MSDN对_snprintf函数的解释:

int _snprintf( char *buffersize_t countconst char *format [, argument] ... ); Parameters
buffer
Storage location for output
count
Maximum number of characters to store
format
Format-control string
argument
Optional arguments
Libraries

All versions of the C run-time libraries.

Return Value

_snprintf returns the number of bytes stored in buffer, not counting the terminating null character. If the number of bytes required to store the data exceeds count, then count bytes of data are stored in buffer and a negative value is returned.

Remarks

The _snprintf function formats and stores count or fewer characters and values (including a terminating null character that is always appended unless count is zero or the formatted string length is greater than or equal to count characters) 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. If copying occurs between strings that overlap, the behavior is undefined.



snprintf跟_snprintf的区别

snprintf函数并不是标准c/c++中规定的函数,但是在许多编译器中,厂商提供了其实现的版本。
在gcc中,该函数名称就snprintf,而在VC中称为_snprintf。
由于不是标准函数,没有一个统一的标准来规定该函数的行为,所以导致了各厂商间的实现版本可
能会有差异。今天也的的确确看到了差异,因为这个小小的差异是我的程序无法正常的处理数据。
这个小小的差异发生在count参数。在VC中,这个count就是要写入的总字符串字符数,例如:
//VC
int main(int argc, char * argv[])
...{
    char   buff[100];
     printf("%d ",_snprintf(buff,10,"1234567890ab"));
     printf("%s",buff);
    return 0;
}

//Linxu:gcc/g++
#include <stdio.h>
int main(int argc, char * argv[])
...{
    char   buff[100];
     printf("%d ",snprintf(buff,10,"1234567890ab"));
     printf("%s",buff);
    return 0;
}
vc程序的输出是:
-1
1234567890@
gcc程序的输出是:
12
123456789
从输出结果可以知道:VC中的_snprintf的count参数表示,会向buff中写入count个字符,不包括'\0'字符,
并且不会在字符串末尾添加'\0'符,并且,如果字符串超过count,函数返回-1以标志可能导致的错误;gcc
中的snprintf函数的count参数表示,向buff中写入10个字符,包括'\0'字符,并且,返回实际的字符串长度,
例子中为12。  如果不了解这些函数在各平台间的差异,也许我们的程序在移植过程中会变得很脆弱。我们应该小心各种各样
的陷阱。

下面是MSDN对_snprintf函数的解释:

int _snprintf( char *buffersize_t countconst char *format [, argument] ... ); Parameters
buffer
Storage location for output
count
Maximum number of characters to store
format
Format-control string
argument
Optional arguments
Libraries

All versions of the C run-time libraries.

Return Value

_snprintf returns the number of bytes stored in buffer, not counting the terminating null character. If the number of bytes required to store the data exceeds count, then count bytes of data are stored in buffer and a negative value is returned.

Remarks

The _snprintf function formats and stores count or fewer characters and values (including a terminating null character that is always appended unless count is zero or the formatted string length is greater than or equal to count characters) 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. If copying occurs between strings that overlap, the behavior is undefined.

snprintf跟_snprintf的区别

snprintf函数并不是标准c/c++中规定的函数,但是在许多编译器中,厂商提供了其实现的版本。
在gcc中,该函数名称就snprintf,而在VC中称为_snprintf。
由于不是标准函数,没有一个统一的标准来规定该函数的行为,所以导致了各厂商间的实现版本可
能会有差异。今天也的的确确看到了差异,因为这个小小的差异是我的程序无法正常的处理数据。
这个小小的差异发生在count参数。在VC中,这个count就是要写入的总字符串字符数,例如:
//VC
int main(int argc, char * argv[])
...{
    char   buff[100];
     printf("%d ",_snprintf(buff,10,"1234567890ab"));
     printf("%s",buff);
    return 0;
}

//Linxu:gcc/g++
#include <stdio.h>
int main(int argc, char * argv[])
...{
    char   buff[100];
     printf("%d ",snprintf(buff,10,"1234567890ab"));
     printf("%s",buff);
    return 0;
}
vc程序的输出是:
-1
1234567890@
gcc程序的输出是:
12
123456789
从输出结果可以知道:VC中的_snprintf的count参数表示,会向buff中写入count个字符,不包括'\0'字符,
并且不会在字符串末尾添加'\0'符,并且,如果字符串超过count,函数返回-1以标志可能导致的错误;gcc
中的snprintf函数的count参数表示,向buff中写入10个字符,包括'\0'字符,并且,返回实际的字符串长度,
例子中为12。  如果不了解这些函数在各平台间的差异,也许我们的程序在移植过程中会变得很脆弱。我们应该小心各种各样
的陷阱。

下面是MSDN对_snprintf函数的解释:

int _snprintf( char *buffersize_t countconst char *format [, argument] ... ); Parameters
buffer
Storage location for output
count
Maximum number of characters to store
format
Format-control string
argument
Optional arguments
Libraries

All versions of the C run-time libraries.

Return Value

_snprintf returns the number of bytes stored in buffer, not counting the terminating null character. If the number of bytes required to store the data exceeds count, then count bytes of data are stored in buffer and a negative value is returned.

Remarks

The _snprintf function formats and stores count or fewer characters and values (including a terminating null character that is always appended unless count is zero or the formatted string length is greater than or equal to count characters) 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. If copying occurs between strings that overlap, the behavior is undefined.


snprintf函数并不是标准c/c++中规定的函数,但是在许多编译器中,厂商提供了其实现的版本。
在gcc中,该函数名称就snprintf,而在VC中称为_snprintf。
由于不是标准函数,没有一个统一的标准来规定该函数的行为,所以导致了各厂商间的实现版本可
能会有差异。今天也的的确确看到了差异,因为这个小小的差异是我的程序无法正常的处理数据。
这个小小的差异发生在count参数。在VC中,这个count就是要写入的总字符串字符数,例如:
//VC

int main(int argc, char * argv[])
...{
    char   buff[100];
     printf("%d ",_snprintf(buff,10,"1234567890ab"));
     printf("%s",buff);
    return 0;
}


//Linxu:gcc/g++

#include <stdio.h>
int main(int argc, char * argv[])
...{
    char   buff[100];
     printf("%d ",snprintf(buff,10,"1234567890ab"));
     printf("%s",buff);
    return 0;
}
vc程序的输出是:
-1
1234567890@
gcc程序的输出是:
12
123456789
从输出结果可以知道:VC中的_snprintf的count参数表示,会向buff中写入count个字符,不包括'\0'字符,
并且不会在字符串末尾添加'\0'符,并且,如果字符串超过count,函数返回-1以标志可能导致的错误;gcc
中的snprintf函数的count参数表示,向buff中写入10个字符,包括'\0'字符,并且,返回实际的字符串长度,
例子中为12。  如果不了解这些函数在各平台间的差异,也许我们的程序在移植过程中会变得很脆弱。我们应该小心各种各样
的陷阱。

下面是MSDN对_snprintf函数的解释:

int _snprintf( char *buffersize_t countconst char *format [, argument] ... ); Parameters
buffer
Storage location for output
count
Maximum number of characters to store
format
Format-control string
argument
Optional arguments
Libraries

All versions of the C run-time libraries.

Return Value

_snprintf returns the number of bytes stored in buffer, not counting the terminating null character. If the number of bytes required to store the data exceeds count, then count bytes of data are stored in buffer and a negative value is returned.

Remarks

The _snprintf function formats and stores count or fewer characters and values (including a terminating null character that is always appended unless count is zero or the formatted string length is greater than or equal to count characters) 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. If copying occurs between strings that overlap, the behavior is undefined.



snprintf跟_snprintf的区别

snprintf函数并不是标准c/c++中规定的函数,但是在许多编译器中,厂商提供了其实现的版本。
在gcc中,该函数名称就snprintf,而在VC中称为_snprintf。
由于不是标准函数,没有一个统一的标准来规定该函数的行为,所以导致了各厂商间的实现版本可
能会有差异。今天也的的确确看到了差异,因为这个小小的差异是我的程序无法正常的处理数据。
这个小小的差异发生在count参数。在VC中,这个count就是要写入的总字符串字符数,例如:
//VC
int main(int argc, char * argv[])
...{
    char   buff[100];
     printf("%d ",_snprintf(buff,10,"1234567890ab"));
     printf("%s",buff);
    return 0;
}

//Linxu:gcc/g++
#include <stdio.h>
int main(int argc, char * argv[])
...{
    char   buff[100];
     printf("%d ",snprintf(buff,10,"1234567890ab"));
     printf("%s",buff);
    return 0;
}
vc程序的输出是:
-1
1234567890@
gcc程序的输出是:
12
123456789
从输出结果可以知道:VC中的_snprintf的count参数表示,会向buff中写入count个字符,不包括'\0'字符,
并且不会在字符串末尾添加'\0'符,并且,如果字符串超过count,函数返回-1以标志可能导致的错误;gcc
中的snprintf函数的count参数表示,向buff中写入10个字符,包括'\0'字符,并且,返回实际的字符串长度,
例子中为12。  如果不了解这些函数在各平台间的差异,也许我们的程序在移植过程中会变得很脆弱。我们应该小心各种各样
的陷阱。

下面是MSDN对_snprintf函数的解释:

int _snprintf( char *buffersize_t countconst char *format [, argument] ... ); Parameters
buffer
Storage location for output
count
Maximum number of characters to store
format
Format-control string
argument
Optional arguments
Libraries

All versions of the C run-time libraries.

Return Value

_snprintf returns the number of bytes stored in buffer, not counting the terminating null character. If the number of bytes required to store the data exceeds count, then count bytes of data are stored in buffer and a negative value is returned.

Remarks

The _snprintf function formats and stores count or fewer characters and values (including a terminating null character that is always appended unless count is zero or the formatted string length is greater than or equal to count characters) 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. If copying occurs between strings that overlap, the behavior is undefined.

snprintf跟_snprintf的区别

snprintf函数并不是标准c/c++中规定的函数,但是在许多编译器中,厂商提供了其实现的版本。
在gcc中,该函数名称就snprintf,而在VC中称为_snprintf。
由于不是标准函数,没有一个统一的标准来规定该函数的行为,所以导致了各厂商间的实现版本可
能会有差异。今天也的的确确看到了差异,因为这个小小的差异是我的程序无法正常的处理数据。
这个小小的差异发生在count参数。在VC中,这个count就是要写入的总字符串字符数,例如:
//VC
int main(int argc, char * argv[])
...{
    char   buff[100];
     printf("%d ",_snprintf(buff,10,"1234567890ab"));
     printf("%s",buff);
    return 0;
}

//Linxu:gcc/g++
#include <stdio.h>
int main(int argc, char * argv[])
...{
    char   buff[100];
     printf("%d ",snprintf(buff,10,"1234567890ab"));
     printf("%s",buff);
    return 0;
}
vc程序的输出是:
-1
1234567890@
gcc程序的输出是:
12
123456789
从输出结果可以知道:VC中的_snprintf的count参数表示,会向buff中写入count个字符,不包括'\0'字符,
并且不会在字符串末尾添加'\0'符,并且,如果字符串超过count,函数返回-1以标志可能导致的错误;gcc
中的snprintf函数的count参数表示,向buff中写入10个字符,包括'\0'字符,并且,返回实际的字符串长度,
例子中为12。  如果不了解这些函数在各平台间的差异,也许我们的程序在移植过程中会变得很脆弱。我们应该小心各种各样
的陷阱。

下面是MSDN对_snprintf函数的解释:

int _snprintf( char *buffersize_t countconst char *format [, argument] ... ); Parameters
buffer
Storage location for output
count
Maximum number of characters to store
format
Format-control string
argument
Optional arguments
Libraries

All versions of the C run-time libraries.

Return Value

_snprintf returns the number of bytes stored in buffer, not counting the terminating null character. If the number of bytes required to store the data exceeds count, then count bytes of data are stored in buffer and a negative value is returned.

Remarks

The _snprintf function formats and stores count or fewer characters and values (including a terminating null character that is always appended unless count is zero or the formatted string length is greater than or equal to count characters) 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. If copying occurs between strings that overlap, the behavior is undefined.


snprintf函数并不是标准c/c++中规定的函数,但是在许多编译器中,厂商提供了其实现的版本。
在gcc中,该函数名称就snprintf,而在VC中称为_snprintf。
由于不是标准函数,没有一个统一的标准来规定该函数的行为,所以导致了各厂商间的实现版本可
能会有差异。今天也的的确确看到了差异,因为这个小小的差异是我的程序无法正常的处理数据。
这个小小的差异发生在count参数。在VC中,这个count就是要写入的总字符串字符数,例如:
//VC

int main(int argc, char * argv[])
...{
    char   buff[100];
     printf("%d ",_snprintf(buff,10,"1234567890ab"));
     printf("%s",buff);
    return 0;
}


//Linxu:gcc/g++

#include <stdio.h>
int main(int argc, char * argv[])
...{
    char   buff[100];
     printf("%d ",snprintf(buff,10,"1234567890ab"));
     printf("%s",buff);
    return 0;
}
vc程序的输出是:
-1
1234567890@
gcc程序的输出是:
12
123456789
从输出结果可以知道:VC中的_snprintf的count参数表示,会向buff中写入count个字符,不包括'\0'字符,
并且不会在字符串末尾添加'\0'符,并且,如果字符串超过count,函数返回-1以标志可能导致的错误;gcc
中的snprintf函数的count参数表示,向buff中写入10个字符,包括'\0'字符,并且,返回实际的字符串长度,
例子中为12。  如果不了解这些函数在各平台间的差异,也许我们的程序在移植过程中会变得很脆弱。我们应该小心各种各样
的陷阱。

下面是MSDN对_snprintf函数的解释:

int _snprintf( char *buffersize_t countconst char *format [, argument] ... ); Parameters
buffer
Storage location for output
count
Maximum number of characters to store
format
Format-control string
argument
Optional arguments
Libraries

All versions of the C run-time libraries.

Return Value

_snprintf returns the number of bytes stored in buffer, not counting the terminating null character. If the number of bytes required to store the data exceeds count, then count bytes of data are stored in buffer and a negative value is returned.

Remarks

The _snprintf function formats and stores count or fewer characters and values (including a terminating null character that is always appended unless count is zero or the formatted string length is greater than or equal to count characters) 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. If copying occurs between strings that overlap, the behavior is undefined.



snprintf跟_snprintf的区别

snprintf函数并不是标准c/c++中规定的函数,但是在许多编译器中,厂商提供了其实现的版本。
在gcc中,该函数名称就snprintf,而在VC中称为_snprintf。
由于不是标准函数,没有一个统一的标准来规定该函数的行为,所以导致了各厂商间的实现版本可
能会有差异。今天也的的确确看到了差异,因为这个小小的差异是我的程序无法正常的处理数据。
这个小小的差异发生在count参数。在VC中,这个count就是要写入的总字符串字符数,例如:
//VC
int main(int argc, char * argv[])
...{
    char   buff[100];
     printf("%d ",_snprintf(buff,10,"1234567890ab"));
     printf("%s",buff);
    return 0;
}

//Linxu:gcc/g++
#include <stdio.h>
int main(int argc, char * argv[])
...{
    char   buff[100];
     printf("%d ",snprintf(buff,10,"1234567890ab"));
     printf("%s",buff);
    return 0;
}
vc程序的输出是:
-1
1234567890@
gcc程序的输出是:
12
123456789
从输出结果可以知道:VC中的_snprintf的count参数表示,会向buff中写入count个字符,不包括'\0'字符,
并且不会在字符串末尾添加'\0'符,并且,如果字符串超过count,函数返回-1以标志可能导致的错误;gcc
中的snprintf函数的count参数表示,向buff中写入10个字符,包括'\0'字符,并且,返回实际的字符串长度,
例子中为12。  如果不了解这些函数在各平台间的差异,也许我们的程序在移植过程中会变得很脆弱。我们应该小心各种各样
的陷阱。

下面是MSDN对_snprintf函数的解释:

int _snprintf( char *buffersize_t countconst char *format [, argument] ... ); Parameters
buffer
Storage location for output
count
Maximum number of characters to store
format
Format-control string
argument
Optional arguments
Libraries

All versions of the C run-time libraries.

Return Value

_snprintf returns the number of bytes stored in buffer, not counting the terminating null character. If the number of bytes required to store the data exceeds count, then count bytes of data are stored in buffer and a negative value is returned.

Remarks

The _snprintf function formats and stores count or fewer characters and values (including a terminating null character that is always appended unless count is zero or the formatted string length is greater than or equal to count characters) 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. If copying occurs between strings that overlap, the behavior is undefined.

snprintf跟_snprintf的区别

snprintf函数并不是标准c/c++中规定的函数,但是在许多编译器中,厂商提供了其实现的版本。
在gcc中,该函数名称就snprintf,而在VC中称为_snprintf。
由于不是标准函数,没有一个统一的标准来规定该函数的行为,所以导致了各厂商间的实现版本可
能会有差异。今天也的的确确看到了差异,因为这个小小的差异是我的程序无法正常的处理数据。
这个小小的差异发生在count参数。在VC中,这个count就是要写入的总字符串字符数,例如:
//VC
int main(int argc, char * argv[])
...{
    char   buff[100];
     printf("%d ",_snprintf(buff,10,"1234567890ab"));
     printf("%s",buff);
    return 0;
}

//Linxu:gcc/g++
#include <stdio.h>
int main(int argc, char * argv[])
...{
    char   buff[100];
     printf("%d ",snprintf(buff,10,"1234567890ab"));
     printf("%s",buff);
    return 0;
}
vc程序的输出是:
-1
1234567890@
gcc程序的输出是:
12
123456789
从输出结果可以知道:VC中的_snprintf的count参数表示,会向buff中写入count个字符,不包括'\0'字符,
并且不会在字符串末尾添加'\0'符,并且,如果字符串超过count,函数返回-1以标志可能导致的错误;gcc
中的snprintf函数的count参数表示,向buff中写入10个字符,包括'\0'字符,并且,返回实际的字符串长度,
例子中为12。  如果不了解这些函数在各平台间的差异,也许我们的程序在移植过程中会变得很脆弱。我们应该小心各种各样
的陷阱。

下面是MSDN对_snprintf函数的解释:

int _snprintf( char *buffersize_t countconst char *format [, argument] ... ); Parameters
buffer
Storage location for output
count
Maximum number of characters to store
format
Format-control string
argument
Optional arguments
Libraries

All versions of the C run-time libraries.

Return Value

_snprintf returns the number of bytes stored in buffer, not counting the terminating null character. If the number of bytes required to store the data exceeds count, then count bytes of data are stored in buffer and a negative value is returned.

Remarks

The _snprintf function formats and stores count or fewer characters and values (including a terminating null character that is always appended unless count is zero or the formatted string length is greater than or equal to count characters) 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. If copying occurs between strings that overlap, the behavior is undefined.


MSDN页面分别如下:

spirntf_s:

http://msdn.microsoft.com/zh-cn/library/ce3zzk1k%28VS.80%29.aspx

_snprintf:

http://msdn.microsoft.com/zh-cn/library/2ts7cx93%28v=VS.90%29.aspx

_snprintf_s:

http://msdn.microsoft.com/zh-cn/library/f30dzcf6.aspx

三个页面都有自己的例子。。其中后2个的例子比较多内容一些。

 

为免将来页面失效:

int sprintf_s(   char *buffer,   size_t sizeOfBuffer,   const char *format [,      argument] ... 

);

template <size_t size>int sprintf_s(   char (&buffer)[size],   const char *format [,      argument] ... 

); // C++ only

int _snprintf(   char *buffer,   size_t count,   const char *format [,      argument] ... 

);

int _snprintf(   char (&buffer)[size],   size_t count,   const char *format [,      argument] ... 

); // C++ only

int _snprintf_s(   char *buffer,   size_t sizeOfBuffer,   size_t count,   const char *format [,   argument] ... 

);

int _snprintf_s(   char (&buffer)[size],   size_t count,   const char *format [,   argument] ... 

); // C++ only

 

呼呼。。内容还挺多。

 

 

这里比较引人注目的是,_snprintf_s为什么在sizeOfBuffer的基础上,还要多加一个count?

count似乎是用来控制理想的宽度的。

如果得到的字符串超过了count,于是会被截断到count的长度后面再加一个null-teminate

当然,更高优先级的应该是sizeOfBuffer,必须不超过这个大小。这个就说到点子上了。

 

如果应该输出的字符串的大小已经达到了sizeOfBuffer,那么就溢出了。溢出的情况下,sprintf_s函数把这当做一个错误,会把buffer缓冲区置为一个空字符串""。

而_snprintf_s的好处就是,有了count参数,输出的字符串就算超过缓冲区长度,仍然会有输出,输出字符串被截断到count大小,在这个大小的字符串后面加null-teminate。

当然,如果count被设置成和sizeOfBuffer同样大,或者不合理的更大,那么这个count参数就失去了意义。

这时候,如果输出字符串将要达到或者超过sizeOfBuffer,一样导致一个错误,输出缓冲区被置为空字符串。

因此,如果希望缓冲区被尽量利用,可以把count参数置为_TRUNCATE,这样的情况下,实际上效果相当于是将count设置为sizeOfBuffer - 1。

 

 

至于C语言环境下,sprintf_s与_snprintf的对比:

注意到,_snprintf的参数用的是count,而sprintf_s的参数用的是sizeOfBuffer。这很能说明问题。

看下对_snprintf的说明:

Let len be the length of the formatted data string (not including the terminating null). len and count are in bytes for _snprintf, wide characters for _snwprintf.

If len < count, then len characters are stored in buffer, a null-terminator is appended, and len is returned.

If len = count, then len characters are stored in buffer, no null-terminator is appended, and len is returned.

If len > count, then count characters are stored in buffer, no null-terminator is appended, and a negative value is returned.

 

也就是说,_snprintf的count参数明明白白的就是一个count。

如果输出字符串刚好达到count,由于期待的最大长度就是count,那么输出字符串肯定要完整,不能截断。

但是假如字符串缓冲区的大小其实就是count,这怎么办?MS VCRT的设计者认为,在这种情况下应该把输出字符串的长度告知调用者,让调用者来决定是否自己添加null-teminate。

换句话说,调用_snprintf时要注意了,必须检查_snprintf的返回值,如果返回值不是正数,那么还得注意你的字符串缓冲区并不是null-teminate结尾的。

 

总结来说,sprintf_s在缓冲区不够大时会失败,失败时缓冲区中是一个空字符串。

_snprintf不会失败,但是必须注意如果缓冲区不够大,缓冲区的内容将不是null-teminate的,必须自己注意字符串的结束。

_snprintf_s结合了2者的优点,只要count参数设置合理,函数就不会因缓冲区不够而失败。

 

但是观察_snprintf_s的说明,有一个很有趣的内容。

这3族函数中,有失败情况的2个函数sprintf_s和_snprintf_s中,(再次强调,我这里的失败的意思是,调用后缓冲区里是一个空字符串),_set_invalid_parameter_handler设置的错误处理器,在失败的情况下会被调用。

 

而截断的情况下,错误处理器并不会被调用。

 

VC的库开发者总是提供一些怪怪的东西。无论如何,让代码更加安全总是符合大家的总体期望的。

另外补充一下,查阅这些字符串安全函数的资料的时候要注意,

对微软来说,凡是限制字符串复制长度的函数,这些设计者仍然认为是不安全的,因为逻辑上来说,

这些长度参数只是限制了源字符串被复制的长度,而不是目标缓冲区的长度。

也就是说,微软的这些设计者认为,安全的方式其实是依赖C++的机制,辨认出目标缓冲区的真正大小,以此实现安全的复制。




snprintf跟_snprintf的区别

snprintf函数并不是标准c/c++中规定的函数,但是在许多编译器中,厂商提供了其实现的版本。
在gcc中,该函数名称就snprintf,而在VC中称为_snprintf。
由于不是标准函数,没有一个统一的标准来规定该函数的行为,所以导致了各厂商间的实现版本可
能会有差异。今天也的的确确看到了差异,因为这个小小的差异是我的程序无法正常的处理数据。
这个小小的差异发生在count参数。在VC中,这个count就是要写入的总字符串字符数,例如:
//VC
int main(int argc, char * argv[])
...{
    char   buff[100];
     printf("%d ",_snprintf(buff,10,"1234567890ab"));
     printf("%s",buff);
    return 0;
}

//Linxu:gcc/g++
#include <stdio.h>
int main(int argc, char * argv[])
...{
    char   buff[100];
     printf("%d ",snprintf(buff,10,"1234567890ab"));
     printf("%s",buff);
    return 0;
}
vc程序的输出是:
-1
1234567890@
gcc程序的输出是:
12
123456789
从输出结果可以知道:VC中的_snprintf的count参数表示,会向buff中写入count个字符,不包括'\0'字符,
并且不会在字符串末尾添加'\0'符,并且,如果字符串超过count,函数返回-1以标志可能导致的错误;gcc
中的snprintf函数的count参数表示,向buff中写入10个字符,包括'\0'字符,并且,返回实际的字符串长度,
例子中为12。  如果不了解这些函数在各平台间的差异,也许我们的程序在移植过程中会变得很脆弱。我们应该小心各种各样
的陷阱。

下面是MSDN对_snprintf函数的解释:

int _snprintf( char *buffersize_t countconst char *format [, argument] ... ); Parameters
buffer
Storage location for output
count
Maximum number of characters to store
format
Format-control string
argument
Optional arguments
Libraries

All versions of the C run-time libraries.

Return Value

_snprintf returns the number of bytes stored in buffer, not counting the terminating null character. If the number of bytes required to store the data exceeds count, then count bytes of data are stored in buffer and a negative value is returned.

Remarks

The _snprintf function formats and stores count or fewer characters and values (including a terminating null character that is always appended unless count is zero or the formatted string length is greater than or equal to count characters) 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. If copying occurs between strings that overlap, the behavior is undefined.

snprintf跟_snprintf的区别

snprintf函数并不是标准c/c++中规定的函数,但是在许多编译器中,厂商提供了其实现的版本。
在gcc中,该函数名称就snprintf,而在VC中称为_snprintf。
由于不是标准函数,没有一个统一的标准来规定该函数的行为,所以导致了各厂商间的实现版本可
能会有差异。今天也的的确确看到了差异,因为这个小小的差异是我的程序无法正常的处理数据。
这个小小的差异发生在count参数。在VC中,这个count就是要写入的总字符串字符数,例如:
//VC
int main(int argc, char * argv[])
...{
    char   buff[100];
     printf("%d ",_snprintf(buff,10,"1234567890ab"));
     printf("%s",buff);
    return 0;
}

//Linxu:gcc/g++
#include <stdio.h>
int main(int argc, char * argv[])
...{
    char   buff[100];
     printf("%d ",snprintf(buff,10,"1234567890ab"));
     printf("%s",buff);
    return 0;
}
vc程序的输出是:
-1
1234567890@
gcc程序的输出是:
12
123456789
从输出结果可以知道:VC中的_snprintf的count参数表示,会向buff中写入count个字符,不包括'\0'字符,
并且不会在字符串末尾添加'\0'符,并且,如果字符串超过count,函数返回-1以标志可能导致的错误;gcc
中的snprintf函数的count参数表示,向buff中写入10个字符,包括'\0'字符,并且,返回实际的字符串长度,
例子中为12。  如果不了解这些函数在各平台间的差异,也许我们的程序在移植过程中会变得很脆弱。我们应该小心各种各样
的陷阱。

下面是MSDN对_snprintf函数的解释:

int _snprintf( char *buffersize_t countconst char *format [, argument] ... ); Parameters
buffer
Storage location for output
count
Maximum number of characters to store
format
Format-control string
argument
Optional arguments
Libraries

All versions of the C run-time libraries.

Return Value

_snprintf returns the number of bytes stored in buffer, not counting the terminating null character. If the number of bytes required to store the data exceeds count, then count bytes of data are stored in buffer and a negative value is returned.

Remarks

The _snprintf function formats and stores count or fewer characters and values (including a terminating null character that is always appended unless count is zero or the formatted string length is greater than or equal to count characters) 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. If copying occurs between strings that overlap, the behavior is undefined.


snprintf函数并不是标准c/c++中规定的函数,但是在许多编译器中,厂商提供了其实现的版本。
在gcc中,该函数名称就snprintf,而在VC中称为_snprintf。
由于不是标准函数,没有一个统一的标准来规定该函数的行为,所以导致了各厂商间的实现版本可
能会有差异。今天也的的确确看到了差异,因为这个小小的差异是我的程序无法正常的处理数据。
这个小小的差异发生在count参数。在VC中,这个count就是要写入的总字符串字符数,例如:
//VC

int main(int argc, char * argv[])
...{
    char   buff[100];
     printf("%d ",_snprintf(buff,10,"1234567890ab"));
     printf("%s",buff);
    return 0;
}


//Linxu:gcc/g++

#include <stdio.h>
int main(int argc, char * argv[])
...{
    char   buff[100];
     printf("%d ",snprintf(buff,10,"1234567890ab"));
     printf("%s",buff);
    return 0;
}
vc程序的输出是:
-1
1234567890@
gcc程序的输出是:
12
123456789
从输出结果可以知道:VC中的_snprintf的count参数表示,会向buff中写入count个字符,不包括'\0'字符,
并且不会在字符串末尾添加'\0'符,并且,如果字符串超过count,函数返回-1以标志可能导致的错误;gcc
中的snprintf函数的count参数表示,向buff中写入10个字符,包括'\0'字符,并且,返回实际的字符串长度,
例子中为12。  如果不了解这些函数在各平台间的差异,也许我们的程序在移植过程中会变得很脆弱。我们应该小心各种各样
的陷阱。

下面是MSDN对_snprintf函数的解释:

int _snprintf( char *buffersize_t countconst char *format [, argument] ... ); Parameters
buffer
Storage location for output
count
Maximum number of characters to store
format
Format-control string
argument
Optional arguments
Libraries

All versions of the C run-time libraries.

Return Value

_snprintf returns the number of bytes stored in buffer, not counting the terminating null character. If the number of bytes required to store the data exceeds count, then count bytes of data are stored in buffer and a negative value is returned.

Remarks

The _snprintf function formats and stores count or fewer characters and values (including a terminating null character that is always appended unless count is zero or the formatted string length is greater than or equal to count characters) 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. If copying occurs between strings that overlap, the behavior is undefined.



snprintf跟_snprintf的区别

snprintf函数并不是标准c/c++中规定的函数,但是在许多编译器中,厂商提供了其实现的版本。
在gcc中,该函数名称就snprintf,而在VC中称为_snprintf。
由于不是标准函数,没有一个统一的标准来规定该函数的行为,所以导致了各厂商间的实现版本可
能会有差异。今天也的的确确看到了差异,因为这个小小的差异是我的程序无法正常的处理数据。
这个小小的差异发生在count参数。在VC中,这个count就是要写入的总字符串字符数,例如:
//VC
int main(int argc, char * argv[])
...{
    char   buff[100];
     printf("%d ",_snprintf(buff,10,"1234567890ab"));
     printf("%s",buff);
    return 0;
}

//Linxu:gcc/g++
#include <stdio.h>
int main(int argc, char * argv[])
...{
    char   buff[100];
     printf("%d ",snprintf(buff,10,"1234567890ab"));
     printf("%s",buff);
    return 0;
}
vc程序的输出是:
-1
1234567890@
gcc程序的输出是:
12
123456789
从输出结果可以知道:VC中的_snprintf的count参数表示,会向buff中写入count个字符,不包括'\0'字符,
并且不会在字符串末尾添加'\0'符,并且,如果字符串超过count,函数返回-1以标志可能导致的错误;gcc
中的snprintf函数的count参数表示,向buff中写入10个字符,包括'\0'字符,并且,返回实际的字符串长度,
例子中为12。  如果不了解这些函数在各平台间的差异,也许我们的程序在移植过程中会变得很脆弱。我们应该小心各种各样
的陷阱。

下面是MSDN对_snprintf函数的解释:

int _snprintf( char *buffersize_t countconst char *format [, argument] ... ); Parameters
buffer
Storage location for output
count
Maximum number of characters to store
format
Format-control string
argument
Optional arguments
Libraries

All versions of the C run-time libraries.

Return Value

_snprintf returns the number of bytes stored in buffer, not counting the terminating null character. If the number of bytes required to store the data exceeds count, then count bytes of data are stored in buffer and a negative value is returned.

Remarks

The _snprintf function formats and stores count or fewer characters and values (including a terminating null character that is always appended unless count is zero or the formatted string length is greater than or equal to count characters) 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. If copying occurs between strings that overlap, the behavior is undefined.

snprintf跟_snprintf的区别

snprintf函数并不是标准c/c++中规定的函数,但是在许多编译器中,厂商提供了其实现的版本。
在gcc中,该函数名称就snprintf,而在VC中称为_snprintf。
由于不是标准函数,没有一个统一的标准来规定该函数的行为,所以导致了各厂商间的实现版本可
能会有差异。今天也的的确确看到了差异,因为这个小小的差异是我的程序无法正常的处理数据。
这个小小的差异发生在count参数。在VC中,这个count就是要写入的总字符串字符数,例如:
//VC
int main(int argc, char * argv[])
...{
    char   buff[100];
     printf("%d ",_snprintf(buff,10,"1234567890ab"));
     printf("%s",buff);
    return 0;
}

//Linxu:gcc/g++
#include <stdio.h>
int main(int argc, char * argv[])
...{
    char   buff[100];
     printf("%d ",snprintf(buff,10,"1234567890ab"));
     printf("%s",buff);
    return 0;
}
vc程序的输出是:
-1
1234567890@
gcc程序的输出是:
12
123456789
从输出结果可以知道:VC中的_snprintf的count参数表示,会向buff中写入count个字符,不包括'\0'字符,
并且不会在字符串末尾添加'\0'符,并且,如果字符串超过count,函数返回-1以标志可能导致的错误;gcc
中的snprintf函数的count参数表示,向buff中写入10个字符,包括'\0'字符,并且,返回实际的字符串长度,
例子中为12。  如果不了解这些函数在各平台间的差异,也许我们的程序在移植过程中会变得很脆弱。我们应该小心各种各样
的陷阱。

下面是MSDN对_snprintf函数的解释:

int _snprintf( char *buffersize_t countconst char *format [, argument] ... ); Parameters
buffer
Storage location for output
count
Maximum number of characters to store
format
Format-control string
argument
Optional arguments
Libraries

All versions of the C run-time libraries.

Return Value

_snprintf returns the number of bytes stored in buffer, not counting the terminating null character. If the number of bytes required to store the data exceeds count, then count bytes of data are stored in buffer and a negative value is returned.

Remarks

The _snprintf function formats and stores count or fewer characters and values (including a terminating null character that is always appended unless count is zero or the formatted string length is greater than or equal to count characters) 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. If copying occurs between strings that overlap, the behavior is undefined.


snprintf函数并不是标准c/c++中规定的函数,但是在许多编译器中,厂商提供了其实现的版本。
在gcc中,该函数名称就snprintf,而在VC中称为_snprintf。
由于不是标准函数,没有一个统一的标准来规定该函数的行为,所以导致了各厂商间的实现版本可
能会有差异。今天也的的确确看到了差异,因为这个小小的差异是我的程序无法正常的处理数据。
这个小小的差异发生在count参数。在VC中,这个count就是要写入的总字符串字符数,例如:
//VC

int main(int argc, char * argv[])
...{
    char   buff[100];
     printf("%d ",_snprintf(buff,10,"1234567890ab"));
     printf("%s",buff);
    return 0;
}


//Linxu:gcc/g++

#include <stdio.h>
int main(int argc, char * argv[])
...{
    char   buff[100];
     printf("%d ",snprintf(buff,10,"1234567890ab"));
     printf("%s",buff);
    return 0;
}
vc程序的输出是:
-1
1234567890@
gcc程序的输出是:
12
123456789
从输出结果可以知道:VC中的_snprintf的count参数表示,会向buff中写入count个字符,不包括'\0'字符,
并且不会在字符串末尾添加'\0'符,并且,如果字符串超过count,函数返回-1以标志可能导致的错误;gcc
中的snprintf函数的count参数表示,向buff中写入10个字符,包括'\0'字符,并且,返回实际的字符串长度,
例子中为12。  如果不了解这些函数在各平台间的差异,也许我们的程序在移植过程中会变得很脆弱。我们应该小心各种各样
的陷阱。

下面是MSDN对_snprintf函数的解释:

int _snprintf( char *buffersize_t countconst char *format [, argument] ... ); Parameters
buffer
Storage location for output
count
Maximum number of characters to store
format
Format-control string
argument
Optional arguments
Libraries

All versions of the C run-time libraries.

Return Value

_snprintf returns the number of bytes stored in buffer, not counting the terminating null character. If the number of bytes required to store the data exceeds count, then count bytes of data are stored in buffer and a negative value is returned.

Remarks

The _snprintf function formats and stores count or fewer characters and values (including a terminating null character that is always appended unless count is zero or the formatted string length is greater than or equal to count characters) 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. If copying occurs between strings that overlap, the behavior is undefined.



snprintf跟_snprintf的区别

snprintf函数并不是标准c/c++中规定的函数,但是在许多编译器中,厂商提供了其实现的版本。
在gcc中,该函数名称就snprintf,而在VC中称为_snprintf。
由于不是标准函数,没有一个统一的标准来规定该函数的行为,所以导致了各厂商间的实现版本可
能会有差异。今天也的的确确看到了差异,因为这个小小的差异是我的程序无法正常的处理数据。
这个小小的差异发生在count参数。在VC中,这个count就是要写入的总字符串字符数,例如:
//VC
int main(int argc, char * argv[])
...{
    char   buff[100];
     printf("%d ",_snprintf(buff,10,"1234567890ab"));
     printf("%s",buff);
    return 0;
}

//Linxu:gcc/g++
#include <stdio.h>
int main(int argc, char * argv[])
...{
    char   buff[100];
     printf("%d ",snprintf(buff,10,"1234567890ab"));
     printf("%s",buff);
    return 0;
}
vc程序的输出是:
-1
1234567890@
gcc程序的输出是:
12
123456789
从输出结果可以知道:VC中的_snprintf的count参数表示,会向buff中写入count个字符,不包括'\0'字符,
并且不会在字符串末尾添加'\0'符,并且,如果字符串超过count,函数返回-1以标志可能导致的错误;gcc
中的snprintf函数的count参数表示,向buff中写入10个字符,包括'\0'字符,并且,返回实际的字符串长度,
例子中为12。  如果不了解这些函数在各平台间的差异,也许我们的程序在移植过程中会变得很脆弱。我们应该小心各种各样
的陷阱。

下面是MSDN对_snprintf函数的解释:

int _snprintf( char *buffersize_t countconst char *format [, argument] ... ); Parameters
buffer
Storage location for output
count
Maximum number of characters to store
format
Format-control string
argument
Optional arguments
Libraries

All versions of the C run-time libraries.

Return Value

_snprintf returns the number of bytes stored in buffer, not counting the terminating null character. If the number of bytes required to store the data exceeds count, then count bytes of data are stored in buffer and a negative value is returned.

Remarks

The _snprintf function formats and stores count or fewer characters and values (including a terminating null character that is always appended unless count is zero or the formatted string length is greater than or equal to count characters) 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. If copying occurs between strings that overlap, the behavior is undefined.

snprintf跟_snprintf的区别

snprintf函数并不是标准c/c++中规定的函数,但是在许多编译器中,厂商提供了其实现的版本。
在gcc中,该函数名称就snprintf,而在VC中称为_snprintf。
由于不是标准函数,没有一个统一的标准来规定该函数的行为,所以导致了各厂商间的实现版本可
能会有差异。今天也的的确确看到了差异,因为这个小小的差异是我的程序无法正常的处理数据。
这个小小的差异发生在count参数。在VC中,这个count就是要写入的总字符串字符数,例如:
//VC
int main(int argc, char * argv[])
...{
    char   buff[100];
     printf("%d ",_snprintf(buff,10,"1234567890ab"));
     printf("%s",buff);
    return 0;
}

//Linxu:gcc/g++
#include <stdio.h>
int main(int argc, char * argv[])
...{
    char   buff[100];
     printf("%d ",snprintf(buff,10,"1234567890ab"));
     printf("%s",buff);
    return 0;
}
vc程序的输出是:
-1
1234567890@
gcc程序的输出是:
12
123456789
从输出结果可以知道:VC中的_snprintf的count参数表示,会向buff中写入count个字符,不包括'\0'字符,
并且不会在字符串末尾添加'\0'符,并且,如果字符串超过count,函数返回-1以标志可能导致的错误;gcc
中的snprintf函数的count参数表示,向buff中写入10个字符,包括'\0'字符,并且,返回实际的字符串长度,
例子中为12。  如果不了解这些函数在各平台间的差异,也许我们的程序在移植过程中会变得很脆弱。我们应该小心各种各样
的陷阱。

下面是MSDN对_snprintf函数的解释:

int _snprintf( char *buffersize_t countconst char *format [, argument] ... ); Parameters
buffer
Storage location for output
count
Maximum number of characters to store
format
Format-control string
argument
Optional arguments
Libraries

All versions of the C run-time libraries.

Return Value

_snprintf returns the number of bytes stored in buffer, not counting the terminating null character. If the number of bytes required to store the data exceeds count, then count bytes of data are stored in buffer and a negative value is returned.

Remarks

The _snprintf function formats and stores count or fewer characters and values (including a terminating null character that is always appended unless count is zero or the formatted string length is greater than or equal to count characters) 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. If copying occurs between strings that overlap, the behavior is undefined.


snprintf函数并不是标准c/c++中规定的函数,但是在许多编译器中,厂商提供了其实现的版本。
在gcc中,该函数名称就snprintf,而在VC中称为_snprintf。
由于不是标准函数,没有一个统一的标准来规定该函数的行为,所以导致了各厂商间的实现版本可
能会有差异。今天也的的确确看到了差异,因为这个小小的差异是我的程序无法正常的处理数据。
这个小小的差异发生在count参数。在VC中,这个count就是要写入的总字符串字符数,例如:
//VC

int main(int argc, char * argv[])
...{
    char   buff[100];
     printf("%d ",_snprintf(buff,10,"1234567890ab"));
     printf("%s",buff);
    return 0;
}


//Linxu:gcc/g++

#include <stdio.h>
int main(int argc, char * argv[])
...{
    char   buff[100];
     printf("%d ",snprintf(buff,10,"1234567890ab"));
     printf("%s",buff);
    return 0;
}
vc程序的输出是:
-1
1234567890@
gcc程序的输出是:
12
123456789
从输出结果可以知道:VC中的_snprintf的count参数表示,会向buff中写入count个字符,不包括'\0'字符,
并且不会在字符串末尾添加'\0'符,并且,如果字符串超过count,函数返回-1以标志可能导致的错误;gcc
中的snprintf函数的count参数表示,向buff中写入10个字符,包括'\0'字符,并且,返回实际的字符串长度,
例子中为12。  如果不了解这些函数在各平台间的差异,也许我们的程序在移植过程中会变得很脆弱。我们应该小心各种各样
的陷阱。

下面是MSDN对_snprintf函数的解释:

int _snprintf( char *buffersize_t countconst char *format [, argument] ... ); Parameters
buffer
Storage location for output
count
Maximum number of characters to store
format
Format-control string
argument
Optional arguments
Libraries

All versions of the C run-time libraries.

Return Value

_snprintf returns the number of bytes stored in buffer, not counting the terminating null character. If the number of bytes required to store the data exceeds count, then count bytes of data are stored in buffer and a negative value is returned.

Remarks

The _snprintf function formats and stores count or fewer characters and values (including a terminating null character that is always appended unless count is zero or the formatted string length is greater than or equal to count characters) 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. If copying occurs between strings that overlap, the behavior is undefined.



snprintf跟_snprintf的区别

snprintf函数并不是标准c/c++中规定的函数,但是在许多编译器中,厂商提供了其实现的版本。
在gcc中,该函数名称就snprintf,而在VC中称为_snprintf。
由于不是标准函数,没有一个统一的标准来规定该函数的行为,所以导致了各厂商间的实现版本可
能会有差异。今天也的的确确看到了差异,因为这个小小的差异是我的程序无法正常的处理数据。
这个小小的差异发生在count参数。在VC中,这个count就是要写入的总字符串字符数,例如:
//VC
int main(int argc, char * argv[])
...{
    char   buff[100];
     printf("%d ",_snprintf(buff,10,"1234567890ab"));
     printf("%s",buff);
    return 0;
}

//Linxu:gcc/g++
#include <stdio.h>
int main(int argc, char * argv[])
...{
    char   buff[100];
     printf("%d ",snprintf(buff,10,"1234567890ab"));
     printf("%s",buff);
    return 0;
}
vc程序的输出是:
-1
1234567890@
gcc程序的输出是:
12
123456789
从输出结果可以知道:VC中的_snprintf的count参数表示,会向buff中写入count个字符,不包括'\0'字符,
并且不会在字符串末尾添加'\0'符,并且,如果字符串超过count,函数返回-1以标志可能导致的错误;gcc
中的snprintf函数的count参数表示,向buff中写入10个字符,包括'\0'字符,并且,返回实际的字符串长度,
例子中为12。  如果不了解这些函数在各平台间的差异,也许我们的程序在移植过程中会变得很脆弱。我们应该小心各种各样
的陷阱。

下面是MSDN对_snprintf函数的解释:

int _snprintf( char *buffersize_t countconst char *format [, argument] ... ); Parameters
buffer
Storage location for output
count
Maximum number of characters to store
format
Format-control string
argument
Optional arguments
Libraries

All versions of the C run-time libraries.

Return Value

_snprintf returns the number of bytes stored in buffer, not counting the terminating null character. If the number of bytes required to store the data exceeds count, then count bytes of data are stored in buffer and a negative value is returned.

Remarks

The _snprintf function formats and stores count or fewer characters and values (including a terminating null character that is always appended unless count is zero or the formatted string length is greater than or equal to count characters) 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. If copying occurs between strings that overlap, the behavior is undefined.

snprintf跟_snprintf的区别

snprintf函数并不是标准c/c++中规定的函数,但是在许多编译器中,厂商提供了其实现的版本。
在gcc中,该函数名称就snprintf,而在VC中称为_snprintf。
由于不是标准函数,没有一个统一的标准来规定该函数的行为,所以导致了各厂商间的实现版本可
能会有差异。今天也的的确确看到了差异,因为这个小小的差异是我的程序无法正常的处理数据。
这个小小的差异发生在count参数。在VC中,这个count就是要写入的总字符串字符数,例如:
//VC
int main(int argc, char * argv[])
...{
    char   buff[100];
     printf("%d ",_snprintf(buff,10,"1234567890ab"));
     printf("%s",buff);
    return 0;
}

//Linxu:gcc/g++
#include <stdio.h>
int main(int argc, char * argv[])
...{
    char   buff[100];
     printf("%d ",snprintf(buff,10,"1234567890ab"));
     printf("%s",buff);
    return 0;
}
vc程序的输出是:
-1
1234567890@
gcc程序的输出是:
12
123456789
从输出结果可以知道:VC中的_snprintf的count参数表示,会向buff中写入count个字符,不包括'\0'字符,
并且不会在字符串末尾添加'\0'符,并且,如果字符串超过count,函数返回-1以标志可能导致的错误;gcc
中的snprintf函数的count参数表示,向buff中写入10个字符,包括'\0'字符,并且,返回实际的字符串长度,
例子中为12。  如果不了解这些函数在各平台间的差异,也许我们的程序在移植过程中会变得很脆弱。我们应该小心各种各样
的陷阱。

下面是MSDN对_snprintf函数的解释:

int _snprintf( char *buffersize_t countconst char *format [, argument] ... ); Parameters
buffer
Storage location for output
count
Maximum number of characters to store
format
Format-control string
argument
Optional arguments
Libraries

All versions of the C run-time libraries.

Return Value

_snprintf returns the number of bytes stored in buffer, not counting the terminating null character. If the number of bytes required to store the data exceeds count, then count bytes of data are stored in buffer and a negative value is returned.

Remarks

The _snprintf function formats and stores count or fewer characters and values (including a terminating null character that is always appended unless count is zero or the formatted string length is greater than or equal to count characters) 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. If copying occurs between strings that overlap, the behavior is undefined.


MSDN页面分别如下:

spirntf_s:

http://msdn.microsoft.com/zh-cn/library/ce3zzk1k%28VS.80%29.aspx

_snprintf:

http://msdn.microsoft.com/zh-cn/library/2ts7cx93%28v=VS.90%29.aspx

_snprintf_s:

http://msdn.microsoft.com/zh-cn/library/f30dzcf6.aspx

三个页面都有自己的例子。。其中后2个的例子比较多内容一些。

 

为免将来页面失效:

int sprintf_s(   char *buffer,   size_t sizeOfBuffer,   const char *format [,      argument] ... 

);

template <size_t size>int sprintf_s(   char (&buffer)[size],   const char *format [,      argument] ... 

); // C++ only

int _snprintf(   char *buffer,   size_t count,   const char *format [,      argument] ... 

);

int _snprintf(   char (&buffer)[size],   size_t count,   const char *format [,      argument] ... 

); // C++ only

int _snprintf_s(   char *buffer,   size_t sizeOfBuffer,   size_t count,   const char *format [,   argument] ... 

);

int _snprintf_s(   char (&buffer)[size],   size_t count,   const char *format [,   argument] ... 

); // C++ only

 

呼呼。。内容还挺多。

 

 

这里比较引人注目的是,_snprintf_s为什么在sizeOfBuffer的基础上,还要多加一个count?

count似乎是用来控制理想的宽度的。

如果得到的字符串超过了count,于是会被截断到count的长度后面再加一个null-teminate

当然,更高优先级的应该是sizeOfBuffer,必须不超过这个大小。这个就说到点子上了。

 

如果应该输出的字符串的大小已经达到了sizeOfBuffer,那么就溢出了。溢出的情况下,sprintf_s函数把这当做一个错误,会把buffer缓冲区置为一个空字符串""。

而_snprintf_s的好处就是,有了count参数,输出的字符串就算超过缓冲区长度,仍然会有输出,输出字符串被截断到count大小,在这个大小的字符串后面加null-teminate。

当然,如果count被设置成和sizeOfBuffer同样大,或者不合理的更大,那么这个count参数就失去了意义。

这时候,如果输出字符串将要达到或者超过sizeOfBuffer,一样导致一个错误,输出缓冲区被置为空字符串。

因此,如果希望缓冲区被尽量利用,可以把count参数置为_TRUNCATE,这样的情况下,实际上效果相当于是将count设置为sizeOfBuffer - 1。

 

 

至于C语言环境下,sprintf_s与_snprintf的对比:

注意到,_snprintf的参数用的是count,而sprintf_s的参数用的是sizeOfBuffer。这很能说明问题。

看下对_snprintf的说明:

Let len be the length of the formatted data string (not including the terminating null). len and count are in bytes for _snprintf, wide characters for _snwprintf.

If len < count, then len characters are stored in buffer, a null-terminator is appended, and len is returned.

If len = count, then len characters are stored in buffer, no null-terminator is appended, and len is returned.

If len > count, then count characters are stored in buffer, no null-terminator is appended, and a negative value is returned.

 

也就是说,_snprintf的count参数明明白白的就是一个count。

如果输出字符串刚好达到count,由于期待的最大长度就是count,那么输出字符串肯定要完整,不能截断。

但是假如字符串缓冲区的大小其实就是count,这怎么办?MS VCRT的设计者认为,在这种情况下应该把输出字符串的长度告知调用者,让调用者来决定是否自己添加null-teminate。

换句话说,调用_snprintf时要注意了,必须检查_snprintf的返回值,如果返回值不是正数,那么还得注意你的字符串缓冲区并不是null-teminate结尾的。

 

总结来说,sprintf_s在缓冲区不够大时会失败,失败时缓冲区中是一个空字符串。

_snprintf不会失败,但是必须注意如果缓冲区不够大,缓冲区的内容将不是null-teminate的,必须自己注意字符串的结束。

_snprintf_s结合了2者的优点,只要count参数设置合理,函数就不会因缓冲区不够而失败。

 

但是观察_snprintf_s的说明,有一个很有趣的内容。

这3族函数中,有失败情况的2个函数sprintf_s和_snprintf_s中,(再次强调,我这里的失败的意思是,调用后缓冲区里是一个空字符串),_set_invalid_parameter_handler设置的错误处理器,在失败的情况下会被调用。

 

而截断的情况下,错误处理器并不会被调用。

 

VC的库开发者总是提供一些怪怪的东西。无论如何,让代码更加安全总是符合大家的总体期望的。

另外补充一下,查阅这些字符串安全函数的资料的时候要注意,

对微软来说,凡是限制字符串复制长度的函数,这些设计者仍然认为是不安全的,因为逻辑上来说,

这些长度参数只是限制了源字符串被复制的长度,而不是目标缓冲区的长度。

也就是说,微软的这些设计者认为,安全的方式其实是依赖C++的机制,辨认出目标缓冲区的真正大小,以此实现安全的复制。




snprintf跟_snprintf的区别

snprintf函数并不是标准c/c++中规定的函数,但是在许多编译器中,厂商提供了其实现的版本。
在gcc中,该函数名称就snprintf,而在VC中称为_snprintf。
由于不是标准函数,没有一个统一的标准来规定该函数的行为,所以导致了各厂商间的实现版本可
能会有差异。今天也的的确确看到了差异,因为这个小小的差异是我的程序无法正常的处理数据。
这个小小的差异发生在count参数。在VC中,这个count就是要写入的总字符串字符数,例如:
//VC
int main(int argc, char * argv[])
...{
    char   buff[100];
     printf("%d ",_snprintf(buff,10,"1234567890ab"));
     printf("%s",buff);
    return 0;
}

//Linxu:gcc/g++
#include <stdio.h>
int main(int argc, char * argv[])
...{
    char   buff[100];
     printf("%d ",snprintf(buff,10,"1234567890ab"));
     printf("%s",buff);
    return 0;
}
vc程序的输出是:
-1
1234567890@
gcc程序的输出是:
12
123456789
从输出结果可以知道:VC中的_snprintf的count参数表示,会向buff中写入count个字符,不包括'\0'字符,
并且不会在字符串末尾添加'\0'符,并且,如果字符串超过count,函数返回-1以标志可能导致的错误;gcc
中的snprintf函数的count参数表示,向buff中写入10个字符,包括'\0'字符,并且,返回实际的字符串长度,
例子中为12。  如果不了解这些函数在各平台间的差异,也许我们的程序在移植过程中会变得很脆弱。我们应该小心各种各样
的陷阱。

下面是MSDN对_snprintf函数的解释:

int _snprintf( char *buffersize_t countconst char *format [, argument] ... ); Parameters
buffer
Storage location for output
count
Maximum number of characters to store
format
Format-control string
argument
Optional arguments
Libraries

All versions of the C run-time libraries.

Return Value

_snprintf returns the number of bytes stored in buffer, not counting the terminating null character. If the number of bytes required to store the data exceeds count, then count bytes of data are stored in buffer and a negative value is returned.

Remarks

The _snprintf function formats and stores count or fewer characters and values (including a terminating null character that is always appended unless count is zero or the formatted string length is greater than or equal to count characters) 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. If copying occurs between strings that overlap, the behavior is undefined.

snprintf跟_snprintf的区别

snprintf函数并不是标准c/c++中规定的函数,但是在许多编译器中,厂商提供了其实现的版本。
在gcc中,该函数名称就snprintf,而在VC中称为_snprintf。
由于不是标准函数,没有一个统一的标准来规定该函数的行为,所以导致了各厂商间的实现版本可
能会有差异。今天也的的确确看到了差异,因为这个小小的差异是我的程序无法正常的处理数据。
这个小小的差异发生在count参数。在VC中,这个count就是要写入的总字符串字符数,例如:
//VC
int main(int argc, char * argv[])
...{
    char   buff[100];
     printf("%d ",_snprintf(buff,10,"1234567890ab"));
     printf("%s",buff);
    return 0;
}

//Linxu:gcc/g++
#include <stdio.h>
int main(int argc, char * argv[])
...{
    char   buff[100];
     printf("%d ",snprintf(buff,10,"1234567890ab"));
     printf("%s",buff);
    return 0;
}
vc程序的输出是:
-1
1234567890@
gcc程序的输出是:
12
123456789
从输出结果可以知道:VC中的_snprintf的count参数表示,会向buff中写入count个字符,不包括'\0'字符,
并且不会在字符串末尾添加'\0'符,并且,如果字符串超过count,函数返回-1以标志可能导致的错误;gcc
中的snprintf函数的count参数表示,向buff中写入10个字符,包括'\0'字符,并且,返回实际的字符串长度,
例子中为12。  如果不了解这些函数在各平台间的差异,也许我们的程序在移植过程中会变得很脆弱。我们应该小心各种各样
的陷阱。

下面是MSDN对_snprintf函数的解释:

int _snprintf( char *buffersize_t countconst char *format [, argument] ... ); Parameters
buffer
Storage location for output
count
Maximum number of characters to store
format
Format-control string
argument
Optional arguments
Libraries

All versions of the C run-time libraries.

Return Value

_snprintf returns the number of bytes stored in buffer, not counting the terminating null character. If the number of bytes required to store the data exceeds count, then count bytes of data are stored in buffer and a negative value is returned.

Remarks

The _snprintf function formats and stores count or fewer characters and values (including a terminating null character that is always appended unless count is zero or the formatted string length is greater than or equal to count characters) 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. If copying occurs between strings that overlap, the behavior is undefined.


snprintf函数并不是标准c/c++中规定的函数,但是在许多编译器中,厂商提供了其实现的版本。
在gcc中,该函数名称就snprintf,而在VC中称为_snprintf。
由于不是标准函数,没有一个统一的标准来规定该函数的行为,所以导致了各厂商间的实现版本可
能会有差异。今天也的的确确看到了差异,因为这个小小的差异是我的程序无法正常的处理数据。
这个小小的差异发生在count参数。在VC中,这个count就是要写入的总字符串字符数,例如:
//VC

int main(int argc, char * argv[])
...{
    char   buff[100];
     printf("%d ",_snprintf(buff,10,"1234567890ab"));
     printf("%s",buff);
    return 0;
}


//Linxu:gcc/g++

#include <stdio.h>
int main(int argc, char * argv[])
...{
    char   buff[100];
     printf("%d ",snprintf(buff,10,"1234567890ab"));
     printf("%s",buff);
    return 0;
}
vc程序的输出是:
-1
1234567890@
gcc程序的输出是:
12
123456789
从输出结果可以知道:VC中的_snprintf的count参数表示,会向buff中写入count个字符,不包括'\0'字符,
并且不会在字符串末尾添加'\0'符,并且,如果字符串超过count,函数返回-1以标志可能导致的错误;gcc
中的snprintf函数的count参数表示,向buff中写入10个字符,包括'\0'字符,并且,返回实际的字符串长度,
例子中为12。  如果不了解这些函数在各平台间的差异,也许我们的程序在移植过程中会变得很脆弱。我们应该小心各种各样
的陷阱。

下面是MSDN对_snprintf函数的解释:

int _snprintf( char *buffersize_t countconst char *format [, argument] ... ); Parameters
buffer
Storage location for output
count
Maximum number of characters to store
format
Format-control string
argument
Optional arguments
Libraries

All versions of the C run-time libraries.

Return Value

_snprintf returns the number of bytes stored in buffer, not counting the terminating null character. If the number of bytes required to store the data exceeds count, then count bytes of data are stored in buffer and a negative value is returned.

Remarks

The _snprintf function formats and stores count or fewer characters and values (including a terminating null character that is always appended unless count is zero or the formatted string length is greater than or equal to count characters) 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. If copying occurs between strings that overlap, the behavior is undefined.



snprintf跟_snprintf的区别

snprintf函数并不是标准c/c++中规定的函数,但是在许多编译器中,厂商提供了其实现的版本。
在gcc中,该函数名称就snprintf,而在VC中称为_snprintf。
由于不是标准函数,没有一个统一的标准来规定该函数的行为,所以导致了各厂商间的实现版本可
能会有差异。今天也的的确确看到了差异,因为这个小小的差异是我的程序无法正常的处理数据。
这个小小的差异发生在count参数。在VC中,这个count就是要写入的总字符串字符数,例如:
//VC
int main(int argc, char * argv[])
...{
    char   buff[100];
     printf("%d ",_snprintf(buff,10,"1234567890ab"));
     printf("%s",buff);
    return 0;
}

//Linxu:gcc/g++
#include <stdio.h>
int main(int argc, char * argv[])
...{
    char   buff[100];
     printf("%d ",snprintf(buff,10,"1234567890ab"));
     printf("%s",buff);
    return 0;
}
vc程序的输出是:
-1
1234567890@
gcc程序的输出是:
12
123456789
从输出结果可以知道:VC中的_snprintf的count参数表示,会向buff中写入count个字符,不包括'\0'字符,
并且不会在字符串末尾添加'\0'符,并且,如果字符串超过count,函数返回-1以标志可能导致的错误;gcc
中的snprintf函数的count参数表示,向buff中写入10个字符,包括'\0'字符,并且,返回实际的字符串长度,
例子中为12。  如果不了解这些函数在各平台间的差异,也许我们的程序在移植过程中会变得很脆弱。我们应该小心各种各样
的陷阱。

下面是MSDN对_snprintf函数的解释:

int _snprintf( char *buffersize_t countconst char *format [, argument] ... ); Parameters
buffer
Storage location for output
count
Maximum number of characters to store
format
Format-control string
argument
Optional arguments
Libraries

All versions of the C run-time libraries.

Return Value

_snprintf returns the number of bytes stored in buffer, not counting the terminating null character. If the number of bytes required to store the data exceeds count, then count bytes of data are stored in buffer and a negative value is returned.

Remarks

The _snprintf function formats and stores count or fewer characters and values (including a terminating null character that is always appended unless count is zero or the formatted string length is greater than or equal to count characters) 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. If copying occurs between strings that overlap, the behavior is undefined.

snprintf跟_snprintf的区别

snprintf函数并不是标准c/c++中规定的函数,但是在许多编译器中,厂商提供了其实现的版本。
在gcc中,该函数名称就snprintf,而在VC中称为_snprintf。
由于不是标准函数,没有一个统一的标准来规定该函数的行为,所以导致了各厂商间的实现版本可
能会有差异。今天也的的确确看到了差异,因为这个小小的差异是我的程序无法正常的处理数据。
这个小小的差异发生在count参数。在VC中,这个count就是要写入的总字符串字符数,例如:
//VC
int main(int argc, char * argv[])
...{
    char   buff[100];
     printf("%d ",_snprintf(buff,10,"1234567890ab"));
     printf("%s",buff);
    return 0;
}

//Linxu:gcc/g++
#include <stdio.h>
int main(int argc, char * argv[])
...{
    char   buff[100];
     printf("%d ",snprintf(buff,10,"1234567890ab"));
     printf("%s",buff);
    return 0;
}
vc程序的输出是:
-1
1234567890@
gcc程序的输出是:
12
123456789
从输出结果可以知道:VC中的_snprintf的count参数表示,会向buff中写入count个字符,不包括'\0'字符,
并且不会在字符串末尾添加'\0'符,并且,如果字符串超过count,函数返回-1以标志可能导致的错误;gcc
中的snprintf函数的count参数表示,向buff中写入10个字符,包括'\0'字符,并且,返回实际的字符串长度,
例子中为12。  如果不了解这些函数在各平台间的差异,也许我们的程序在移植过程中会变得很脆弱。我们应该小心各种各样
的陷阱。

下面是MSDN对_snprintf函数的解释:

int _snprintf( char *buffersize_t countconst char *format [, argument] ... ); Parameters
buffer
Storage location for output
count
Maximum number of characters to store
format
Format-control string
argument
Optional arguments
Libraries

All versions of the C run-time libraries.

Return Value

_snprintf returns the number of bytes stored in buffer, not counting the terminating null character. If the number of bytes required to store the data exceeds count, then count bytes of data are stored in buffer and a negative value is returned.

Remarks

The _snprintf function formats and stores count or fewer characters and values (including a terminating null character that is always appended unless count is zero or the formatted string length is greater than or equal to count characters) 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. If copying occurs between strings that overlap, the behavior is undefined.


snprintf函数并不是标准c/c++中规定的函数,但是在许多编译器中,厂商提供了其实现的版本。
在gcc中,该函数名称就snprintf,而在VC中称为_snprintf。
由于不是标准函数,没有一个统一的标准来规定该函数的行为,所以导致了各厂商间的实现版本可
能会有差异。今天也的的确确看到了差异,因为这个小小的差异是我的程序无法正常的处理数据。
这个小小的差异发生在count参数。在VC中,这个count就是要写入的总字符串字符数,例如:
//VC

int main(int argc, char * argv[])
...{
    char   buff[100];
     printf("%d ",_snprintf(buff,10,"1234567890ab"));
     printf("%s",buff);
    return 0;
}


//Linxu:gcc/g++

#include <stdio.h>
int main(int argc, char * argv[])
...{
    char   buff[100];
     printf("%d ",snprintf(buff,10,"1234567890ab"));
     printf("%s",buff);
    return 0;
}
vc程序的输出是:
-1
1234567890@
gcc程序的输出是:
12
123456789
从输出结果可以知道:VC中的_snprintf的count参数表示,会向buff中写入count个字符,不包括'\0'字符,
并且不会在字符串末尾添加'\0'符,并且,如果字符串超过count,函数返回-1以标志可能导致的错误;gcc
中的snprintf函数的count参数表示,向buff中写入10个字符,包括'\0'字符,并且,返回实际的字符串长度,
例子中为12。  如果不了解这些函数在各平台间的差异,也许我们的程序在移植过程中会变得很脆弱。我们应该小心各种各样
的陷阱。

下面是MSDN对_snprintf函数的解释:

int _snprintf( char *buffersize_t countconst char *format [, argument] ... ); Parameters
buffer
Storage location for output
count
Maximum number of characters to store
format
Format-control string
argument
Optional arguments
Libraries

All versions of the C run-time libraries.

Return Value

_snprintf returns the number of bytes stored in buffer, not counting the terminating null character. If the number of bytes required to store the data exceeds count, then count bytes of data are stored in buffer and a negative value is returned.

Remarks

The _snprintf function formats and stores count or fewer characters and values (including a terminating null character that is always appended unless count is zero or the formatted string length is greater than or equal to count characters) 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. If copying occurs between strings that overlap, the behavior is undefined.



snprintf跟_snprintf的区别

snprintf函数并不是标准c/c++中规定的函数,但是在许多编译器中,厂商提供了其实现的版本。
在gcc中,该函数名称就snprintf,而在VC中称为_snprintf。
由于不是标准函数,没有一个统一的标准来规定该函数的行为,所以导致了各厂商间的实现版本可
能会有差异。今天也的的确确看到了差异,因为这个小小的差异是我的程序无法正常的处理数据。
这个小小的差异发生在count参数。在VC中,这个count就是要写入的总字符串字符数,例如:
//VC
int main(int argc, char * argv[])
...{
    char   buff[100];
     printf("%d ",_snprintf(buff,10,"1234567890ab"));
     printf("%s",buff);
    return 0;
}

//Linxu:gcc/g++
#include <stdio.h>
int main(int argc, char * argv[])
...{
    char   buff[100];
     printf("%d ",snprintf(buff,10,"1234567890ab"));
     printf("%s",buff);
    return 0;
}
vc程序的输出是:
-1
1234567890@
gcc程序的输出是:
12
123456789
从输出结果可以知道:VC中的_snprintf的count参数表示,会向buff中写入count个字符,不包括'\0'字符,
并且不会在字符串末尾添加'\0'符,并且,如果字符串超过count,函数返回-1以标志可能导致的错误;gcc
中的snprintf函数的count参数表示,向buff中写入10个字符,包括'\0'字符,并且,返回实际的字符串长度,
例子中为12。  如果不了解这些函数在各平台间的差异,也许我们的程序在移植过程中会变得很脆弱。我们应该小心各种各样
的陷阱。

下面是MSDN对_snprintf函数的解释:

int _snprintf( char *buffersize_t countconst char *format [, argument] ... ); Parameters
buffer
Storage location for output
count
Maximum number of characters to store
format
Format-control string
argument
Optional arguments
Libraries

All versions of the C run-time libraries.

Return Value

_snprintf returns the number of bytes stored in buffer, not counting the terminating null character. If the number of bytes required to store the data exceeds count, then count bytes of data are stored in buffer and a negative value is returned.

Remarks

The _snprintf function formats and stores count or fewer characters and values (including a terminating null character that is always appended unless count is zero or the formatted string length is greater than or equal to count characters) 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. If copying occurs between strings that overlap, the behavior is undefined.

snprintf跟_snprintf的区别

snprintf函数并不是标准c/c++中规定的函数,但是在许多编译器中,厂商提供了其实现的版本。
在gcc中,该函数名称就snprintf,而在VC中称为_snprintf。
由于不是标准函数,没有一个统一的标准来规定该函数的行为,所以导致了各厂商间的实现版本可
能会有差异。今天也的的确确看到了差异,因为这个小小的差异是我的程序无法正常的处理数据。
这个小小的差异发生在count参数。在VC中,这个count就是要写入的总字符串字符数,例如:
//VC
int main(int argc, char * argv[])
...{
    char   buff[100];
     printf("%d ",_snprintf(buff,10,"1234567890ab"));
     printf("%s",buff);
    return 0;
}

//Linxu:gcc/g++
#include <stdio.h>
int main(int argc, char * argv[])
...{
    char   buff[100];
     printf("%d ",snprintf(buff,10,"1234567890ab"));
     printf("%s",buff);
    return 0;
}
vc程序的输出是:
-1
1234567890@
gcc程序的输出是:
12
123456789
从输出结果可以知道:VC中的_snprintf的count参数表示,会向buff中写入count个字符,不包括'\0'字符,
并且不会在字符串末尾添加'\0'符,并且,如果字符串超过count,函数返回-1以标志可能导致的错误;gcc
中的snprintf函数的count参数表示,向buff中写入10个字符,包括'\0'字符,并且,返回实际的字符串长度,
例子中为12。  如果不了解这些函数在各平台间的差异,也许我们的程序在移植过程中会变得很脆弱。我们应该小心各种各样
的陷阱。

下面是MSDN对_snprintf函数的解释:

int _snprintf( char *buffersize_t countconst char *format [, argument] ... ); Parameters
buffer
Storage location for output
count
Maximum number of characters to store
format
Format-control string
argument
Optional arguments
Libraries

All versions of the C run-time libraries.

Return Value

_snprintf returns the number of bytes stored in buffer, not counting the terminating null character. If the number of bytes required to store the data exceeds count, then count bytes of data are stored in buffer and a negative value is returned.

Remarks

The _snprintf function formats and stores count or fewer characters and values (including a terminating null character that is always appended unless count is zero or the formatted string length is greater than or equal to count characters) 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. If copying occurs between strings that overlap, the behavior is undefined.


snprintf函数并不是标准c/c++中规定的函数,但是在许多编译器中,厂商提供了其实现的版本。
在gcc中,该函数名称就snprintf,而在VC中称为_snprintf。
由于不是标准函数,没有一个统一的标准来规定该函数的行为,所以导致了各厂商间的实现版本可
能会有差异。今天也的的确确看到了差异,因为这个小小的差异是我的程序无法正常的处理数据。
这个小小的差异发生在count参数。在VC中,这个count就是要写入的总字符串字符数,例如:
//VC

int main(int argc, char * argv[])
...{
    char   buff[100];
     printf("%d ",_snprintf(buff,10,"1234567890ab"));
     printf("%s",buff);
    return 0;
}


//Linxu:gcc/g++

#include <stdio.h>
int main(int argc, char * argv[])
...{
    char   buff[100];
     printf("%d ",snprintf(buff,10,"1234567890ab"));
     printf("%s",buff);
    return 0;
}
vc程序的输出是:
-1
1234567890@
gcc程序的输出是:
12
123456789
从输出结果可以知道:VC中的_snprintf的count参数表示,会向buff中写入count个字符,不包括'\0'字符,
并且不会在字符串末尾添加'\0'符,并且,如果字符串超过count,函数返回-1以标志可能导致的错误;gcc
中的snprintf函数的count参数表示,向buff中写入10个字符,包括'\0'字符,并且,返回实际的字符串长度,
例子中为12。  如果不了解这些函数在各平台间的差异,也许我们的程序在移植过程中会变得很脆弱。我们应该小心各种各样
的陷阱。

下面是MSDN对_snprintf函数的解释:

int _snprintf( char *buffersize_t countconst char *format [, argument] ... ); Parameters
buffer
Storage location for output
count
Maximum number of characters to store
format
Format-control string
argument
Optional arguments
Libraries

All versions of the C run-time libraries.

Return Value

_snprintf returns the number of bytes stored in buffer, not counting the terminating null character. If the number of bytes required to store the data exceeds count, then count bytes of data are stored in buffer and a negative value is returned.

Remarks

The _snprintf function formats and stores count or fewer characters and values (including a terminating null character that is always appended unless count is zero or the formatted string length is greater than or equal to count characters) 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. If copying occurs between strings that overlap, the behavior is undefined.



snprintf跟_snprintf的区别

snprintf函数并不是标准c/c++中规定的函数,但是在许多编译器中,厂商提供了其实现的版本。
在gcc中,该函数名称就snprintf,而在VC中称为_snprintf。
由于不是标准函数,没有一个统一的标准来规定该函数的行为,所以导致了各厂商间的实现版本可
能会有差异。今天也的的确确看到了差异,因为这个小小的差异是我的程序无法正常的处理数据。
这个小小的差异发生在count参数。在VC中,这个count就是要写入的总字符串字符数,例如:
//VC
int main(int argc, char * argv[])
...{
    char   buff[100];
     printf("%d ",_snprintf(buff,10,"1234567890ab"));
     printf("%s",buff);
    return 0;
}

//Linxu:gcc/g++
#include <stdio.h>
int main(int argc, char * argv[])
...{
    char   buff[100];
     printf("%d ",snprintf(buff,10,"1234567890ab"));
     printf("%s",buff);
    return 0;
}
vc程序的输出是:
-1
1234567890@
gcc程序的输出是:
12
123456789
从输出结果可以知道:VC中的_snprintf的count参数表示,会向buff中写入count个字符,不包括'\0'字符,
并且不会在字符串末尾添加'\0'符,并且,如果字符串超过count,函数返回-1以标志可能导致的错误;gcc
中的snprintf函数的count参数表示,向buff中写入10个字符,包括'\0'字符,并且,返回实际的字符串长度,
例子中为12。  如果不了解这些函数在各平台间的差异,也许我们的程序在移植过程中会变得很脆弱。我们应该小心各种各样
的陷阱。

下面是MSDN对_snprintf函数的解释:

int _snprintf( char *buffersize_t countconst char *format [, argument] ... ); Parameters
buffer
Storage location for output
count
Maximum number of characters to store
format
Format-control string
argument
Optional arguments
Libraries

All versions of the C run-time libraries.

Return Value

_snprintf returns the number of bytes stored in buffer, not counting the terminating null character. If the number of bytes required to store the data exceeds count, then count bytes of data are stored in buffer and a negative value is returned.

Remarks

The _snprintf function formats and stores count or fewer characters and values (including a terminating null character that is always appended unless count is zero or the formatted string length is greater than or equal to count characters) 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. If copying occurs between strings that overlap, the behavior is undefined.

snprintf跟_snprintf的区别

snprintf函数并不是标准c/c++中规定的函数,但是在许多编译器中,厂商提供了其实现的版本。
在gcc中,该函数名称就snprintf,而在VC中称为_snprintf。
由于不是标准函数,没有一个统一的标准来规定该函数的行为,所以导致了各厂商间的实现版本可
能会有差异。今天也的的确确看到了差异,因为这个小小的差异是我的程序无法正常的处理数据。
这个小小的差异发生在count参数。在VC中,这个count就是要写入的总字符串字符数,例如:
//VC
int main(int argc, char * argv[])
...{
    char   buff[100];
     printf("%d ",_snprintf(buff,10,"1234567890ab"));
     printf("%s",buff);
    return 0;
}

//Linxu:gcc/g++
#include <stdio.h>
int main(int argc, char * argv[])
...{
    char   buff[100];
     printf("%d ",snprintf(buff,10,"1234567890ab"));
     printf("%s",buff);
    return 0;
}
vc程序的输出是:
-1
1234567890@
gcc程序的输出是:
12
123456789
从输出结果可以知道:VC中的_snprintf的count参数表示,会向buff中写入count个字符,不包括'\0'字符,
并且不会在字符串末尾添加'\0'符,并且,如果字符串超过count,函数返回-1以标志可能导致的错误;gcc
中的snprintf函数的count参数表示,向buff中写入10个字符,包括'\0'字符,并且,返回实际的字符串长度,
例子中为12。  如果不了解这些函数在各平台间的差异,也许我们的程序在移植过程中会变得很脆弱。我们应该小心各种各样
的陷阱。

下面是MSDN对_snprintf函数的解释:

int _snprintf( char *buffersize_t countconst char *format [, argument] ... ); Parameters
buffer
Storage location for output
count
Maximum number of characters to store
format
Format-control string
argument
Optional arguments
Libraries

All versions of the C run-time libraries.

Return Value

_snprintf returns the number of bytes stored in buffer, not counting the terminating null character. If the number of bytes required to store the data exceeds count, then count bytes of data are stored in buffer and a negative value is returned.

Remarks

The _snprintf function formats and stores count or fewer characters and values (including a terminating null character that is always appended unless count is zero or the formatted string length is greater than or equal to count characters) 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. If copying occurs between strings that overlap, the behavior is undefined.


snprintf函数并不是标准c/c++中规定的函数,但是在许多编译器中,厂商提供了其实现的版本。
在gcc中,该函数名称就snprintf,而在VC中称为_snprintf。
由于不是标准函数,没有一个统一的标准来规定该函数的行为,所以导致了各厂商间的实现版本可
能会有差异。今天也的的确确看到了差异,因为这个小小的差异是我的程序无法正常的处理数据。
这个小小的差异发生在count参数。在VC中,这个count就是要写入的总字符串字符数,例如:
//VC

int main(int argc, char * argv[])
...{
    char   buff[100];
     printf("%d ",_snprintf(buff,10,"1234567890ab"));
     printf("%s",buff);
    return 0;
}


//Linxu:gcc/g++

#include <stdio.h>
int main(int argc, char * argv[])
...{
    char   buff[100];
     printf("%d ",snprintf(buff,10,"1234567890ab"));
     printf("%s",buff);
    return 0;
}
vc程序的输出是:
-1
1234567890@
gcc程序的输出是:
12
123456789
从输出结果可以知道:VC中的_snprintf的count参数表示,会向buff中写入count个字符,不包括'\0'字符,
并且不会在字符串末尾添加'\0'符,并且,如果字符串超过count,函数返回-1以标志可能导致的错误;gcc
中的snprintf函数的count参数表示,向buff中写入10个字符,包括'\0'字符,并且,返回实际的字符串长度,
例子中为12。  如果不了解这些函数在各平台间的差异,也许我们的程序在移植过程中会变得很脆弱。我们应该小心各种各样
的陷阱。

下面是MSDN对_snprintf函数的解释:

int _snprintf( char *buffersize_t countconst char *format [, argument] ... ); Parameters
buffer
Storage location for output
count
Maximum number of characters to store
format
Format-control string
argument
Optional arguments
Libraries

All versions of the C run-time libraries.

Return Value

_snprintf returns the number of bytes stored in buffer, not counting the terminating null character. If the number of bytes required to store the data exceeds count, then count bytes of data are stored in buffer and a negative value is returned.

Remarks

The _snprintf function formats and stores count or fewer characters and values (including a terminating null character that is always appended unless count is zero or the formatted string length is greater than or equal to count characters) 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. If copying occurs between strings that overlap, the behavior is undefined.



snprintf跟_snprintf的区别

snprintf函数并不是标准c/c++中规定的函数,但是在许多编译器中,厂商提供了其实现的版本。
在gcc中,该函数名称就snprintf,而在VC中称为_snprintf。
由于不是标准函数,没有一个统一的标准来规定该函数的行为,所以导致了各厂商间的实现版本可
能会有差异。今天也的的确确看到了差异,因为这个小小的差异是我的程序无法正常的处理数据。
这个小小的差异发生在count参数。在VC中,这个count就是要写入的总字符串字符数,例如:
//VC
int main(int argc, char * argv[])
...{
    char   buff[100];
     printf("%d ",_snprintf(buff,10,"1234567890ab"));
     printf("%s",buff);
    return 0;
}

//Linxu:gcc/g++
#include <stdio.h>
int main(int argc, char * argv[])
...{
    char   buff[100];
     printf("%d ",snprintf(buff,10,"1234567890ab"));
     printf("%s",buff);
    return 0;
}
vc程序的输出是:
-1
1234567890@
gcc程序的输出是:
12
123456789
从输出结果可以知道:VC中的_snprintf的count参数表示,会向buff中写入count个字符,不包括'\0'字符,
并且不会在字符串末尾添加'\0'符,并且,如果字符串超过count,函数返回-1以标志可能导致的错误;gcc
中的snprintf函数的count参数表示,向buff中写入10个字符,包括'\0'字符,并且,返回实际的字符串长度,
例子中为12。  如果不了解这些函数在各平台间的差异,也许我们的程序在移植过程中会变得很脆弱。我们应该小心各种各样
的陷阱。

下面是MSDN对_snprintf函数的解释:

int _snprintf( char *buffersize_t countconst char *format [, argument] ... ); Parameters
buffer
Storage location for output
count
Maximum number of characters to store
format
Format-control string
argument
Optional arguments
Libraries

All versions of the C run-time libraries.

Return Value

_snprintf returns the number of bytes stored in buffer, not counting the terminating null character. If the number of bytes required to store the data exceeds count, then count bytes of data are stored in buffer and a negative value is returned.

Remarks

The _snprintf function formats and stores count or fewer characters and values (including a terminating null character that is always appended unless count is zero or the formatted string length is greater than or equal to count characters) 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. If copying occurs between strings that overlap, the behavior is undefined.

snprintf跟_snprintf的区别

snprintf函数并不是标准c/c++中规定的函数,但是在许多编译器中,厂商提供了其实现的版本。
在gcc中,该函数名称就snprintf,而在VC中称为_snprintf。
由于不是标准函数,没有一个统一的标准来规定该函数的行为,所以导致了各厂商间的实现版本可
能会有差异。今天也的的确确看到了差异,因为这个小小的差异是我的程序无法正常的处理数据。
这个小小的差异发生在count参数。在VC中,这个count就是要写入的总字符串字符数,例如:
//VC
int main(int argc, char * argv[])
...{
    char   buff[100];
     printf("%d ",_snprintf(buff,10,"1234567890ab"));
     printf("%s",buff);
    return 0;
}

//Linxu:gcc/g++
#include <stdio.h>
int main(int argc, char * argv[])
...{
    char   buff[100];
     printf("%d ",snprintf(buff,10,"1234567890ab"));
     printf("%s",buff);
    return 0;
}
vc程序的输出是:
-1
1234567890@
gcc程序的输出是:
12
123456789
从输出结果可以知道:VC中的_snprintf的count参数表示,会向buff中写入count个字符,不包括'\0'字符,
并且不会在字符串末尾添加'\0'符,并且,如果字符串超过count,函数返回-1以标志可能导致的错误;gcc
中的snprintf函数的count参数表示,向buff中写入10个字符,包括'\0'字符,并且,返回实际的字符串长度,
例子中为12。  如果不了解这些函数在各平台间的差异,也许我们的程序在移植过程中会变得很脆弱。我们应该小心各种各样
的陷阱。

下面是MSDN对_snprintf函数的解释:

int _snprintf( char *buffersize_t countconst char *format [, argument] ... ); Parameters
buffer
Storage location for output
count
Maximum number of characters to store
format
Format-control string
argument
Optional arguments
Libraries

All versions of the C run-time libraries.

Return Value

_snprintf returns the number of bytes stored in buffer, not counting the terminating null character. If the number of bytes required to store the data exceeds count, then count bytes of data are stored in buffer and a negative value is returned.

Remarks

The _snprintf function formats and stores count or fewer characters and values (including a terminating null character that is always appended unless count is zero or the formatted string length is greater than or equal to count characters) 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. If copying occurs between strings that overlap, the behavior is undefined.


snprintf函数并不是标准c/c++中规定的函数,但是在许多编译器中,厂商提供了其实现的版本。
在gcc中,该函数名称就snprintf,而在VC中称为_snprintf。
由于不是标准函数,没有一个统一的标准来规定该函数的行为,所以导致了各厂商间的实现版本可
能会有差异。今天也的的确确看到了差异,因为这个小小的差异是我的程序无法正常的处理数据。
这个小小的差异发生在count参数。在VC中,这个count就是要写入的总字符串字符数,例如:
//VC

int main(int argc, char * argv[])
...{
    char   buff[100];
     printf("%d ",_snprintf(buff,10,"1234567890ab"));
     printf("%s",buff);
    return 0;
}


//Linxu:gcc/g++

#include <stdio.h>
int main(int argc, char * argv[])
...{
    char   buff[100];
     printf("%d ",snprintf(buff,10,"1234567890ab"));
     printf("%s",buff);
    return 0;
}
vc程序的输出是:
-1
1234567890@
gcc程序的输出是:
12
123456789
从输出结果可以知道:VC中的_snprintf的count参数表示,会向buff中写入count个字符,不包括'\0'字符,
并且不会在字符串末尾添加'\0'符,并且,如果字符串超过count,函数返回-1以标志可能导致的错误;gcc
中的snprintf函数的count参数表示,向buff中写入10个字符,包括'\0'字符,并且,返回实际的字符串长度,
例子中为12。  如果不了解这些函数在各平台间的差异,也许我们的程序在移植过程中会变得很脆弱。我们应该小心各种各样
的陷阱。

下面是MSDN对_snprintf函数的解释:

int _snprintf( char *buffersize_t countconst char *format [, argument] ... ); Parameters
buffer
Storage location for output
count
Maximum number of characters to store
format
Format-control string
argument
Optional arguments
Libraries

All versions of the C run-time libraries.

Return Value

_snprintf returns the number of bytes stored in buffer, not counting the terminating null character. If the number of bytes required to store the data exceeds count, then count bytes of data are stored in buffer and a negative value is returned.

Remarks

The _snprintf function formats and stores count or fewer characters and values (including a terminating null character that is always appended unless count is zero or the formatted string length is greater than or equal to count characters) 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. If copying occurs between strings that overlap, the behavior is undefined.



snprintf跟_snprintf的区别

snprintf函数并不是标准c/c++中规定的函数,但是在许多编译器中,厂商提供了其实现的版本。
在gcc中,该函数名称就snprintf,而在VC中称为_snprintf。
由于不是标准函数,没有一个统一的标准来规定该函数的行为,所以导致了各厂商间的实现版本可
能会有差异。今天也的的确确看到了差异,因为这个小小的差异是我的程序无法正常的处理数据。
这个小小的差异发生在count参数。在VC中,这个count就是要写入的总字符串字符数,例如:
//VC
int main(int argc, char * argv[])
...{
    char   buff[100];
     printf("%d ",_snprintf(buff,10,"1234567890ab"));
     printf("%s",buff);
    return 0;
}

//Linxu:gcc/g++
#include <stdio.h>
int main(int argc, char * argv[])
...{
    char   buff[100];
     printf("%d ",snprintf(buff,10,"1234567890ab"));
     printf("%s",buff);
    return 0;
}
vc程序的输出是:
-1
1234567890@
gcc程序的输出是:
12
123456789
从输出结果可以知道:VC中的_snprintf的count参数表示,会向buff中写入count个字符,不包括'\0'字符,
并且不会在字符串末尾添加'\0'符,并且,如果字符串超过count,函数返回-1以标志可能导致的错误;gcc
中的snprintf函数的count参数表示,向buff中写入10个字符,包括'\0'字符,并且,返回实际的字符串长度,
例子中为12。  如果不了解这些函数在各平台间的差异,也许我们的程序在移植过程中会变得很脆弱。我们应该小心各种各样
的陷阱。

下面是MSDN对_snprintf函数的解释:

int _snprintf( char *buffersize_t countconst char *format [, argument] ... ); Parameters
buffer
Storage location for output
count
Maximum number of characters to store
format
Format-control string
argument
Optional arguments
Libraries

All versions of the C run-time libraries.

Return Value

_snprintf returns the number of bytes stored in buffer, not counting the terminating null character. If the number of bytes required to store the data exceeds count, then count bytes of data are stored in buffer and a negative value is returned.

Remarks

The _snprintf function formats and stores count or fewer characters and values (including a terminating null character that is always appended unless count is zero or the formatted string length is greater than or equal to count characters) 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. If copying occurs between strings that overlap, the behavior is undefined.

snprintf跟_snprintf的区别

snprintf函数并不是标准c/c++中规定的函数,但是在许多编译器中,厂商提供了其实现的版本。
在gcc中,该函数名称就snprintf,而在VC中称为_snprintf。
由于不是标准函数,没有一个统一的标准来规定该函数的行为,所以导致了各厂商间的实现版本可
能会有差异。今天也的的确确看到了差异,因为这个小小的差异是我的程序无法正常的处理数据。
这个小小的差异发生在count参数。在VC中,这个count就是要写入的总字符串字符数,例如:
//VC
int main(int argc, char * argv[])
...{
    char   buff[100];
     printf("%d ",_snprintf(buff,10,"1234567890ab"));
     printf("%s",buff);
    return 0;
}

//Linxu:gcc/g++
#include <stdio.h>
int main(int argc, char * argv[])
...{
    char   buff[100];
     printf("%d ",snprintf(buff,10,"1234567890ab"));
     printf("%s",buff);
    return 0;
}
vc程序的输出是:
-1
1234567890@
gcc程序的输出是:
12
123456789
从输出结果可以知道:VC中的_snprintf的count参数表示,会向buff中写入count个字符,不包括'\0'字符,
并且不会在字符串末尾添加'\0'符,并且,如果字符串超过count,函数返回-1以标志可能导致的错误;gcc
中的snprintf函数的count参数表示,向buff中写入10个字符,包括'\0'字符,并且,返回实际的字符串长度,
例子中为12。  如果不了解这些函数在各平台间的差异,也许我们的程序在移植过程中会变得很脆弱。我们应该小心各种各样
的陷阱。

下面是MSDN对_snprintf函数的解释:

int _snprintf( char *buffersize_t countconst char *format [, argument] ... ); Parameters
buffer
Storage location for output
count
Maximum number of characters to store
format
Format-control string
argument
Optional arguments
Libraries

All versions of the C run-time libraries.

Return Value

_snprintf returns the number of bytes stored in buffer, not counting the terminating null character. If the number of bytes required to store the data exceeds count, then count bytes of data are stored in buffer and a negative value is returned.

Remarks

The _snprintf function formats and stores count or fewer characters and values (including a terminating null character that is always appended unless count is zero or the formatted string length is greater than or equal to count characters) 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. If copying occurs between strings that overlap, the behavior is undefined.


snprintf函数并不是标准c/c++中规定的函数,但是在许多编译器中,厂商提供了其实现的版本。
在gcc中,该函数名称就snprintf,而在VC中称为_snprintf。
由于不是标准函数,没有一个统一的标准来规定该函数的行为,所以导致了各厂商间的实现版本可
能会有差异。今天也的的确确看到了差异,因为这个小小的差异是我的程序无法正常的处理数据。
这个小小的差异发生在count参数。在VC中,这个count就是要写入的总字符串字符数,例如:
//VC

int main(int argc, char * argv[])
...{
    char   buff[100];
     printf("%d ",_snprintf(buff,10,"1234567890ab"));
     printf("%s",buff);
    return 0;
}


//Linxu:gcc/g++

#include <stdio.h>
int main(int argc, char * argv[])
...{
    char   buff[100];
     printf("%d ",snprintf(buff,10,"1234567890ab"));
     printf("%s",buff);
    return 0;
}
vc程序的输出是:
-1
1234567890@
gcc程序的输出是:
12
123456789
从输出结果可以知道:VC中的_snprintf的count参数表示,会向buff中写入count个字符,不包括'\0'字符,
并且不会在字符串末尾添加'\0'符,并且,如果字符串超过count,函数返回-1以标志可能导致的错误;gcc
中的snprintf函数的count参数表示,向buff中写入10个字符,包括'\0'字符,并且,返回实际的字符串长度,
例子中为12。  如果不了解这些函数在各平台间的差异,也许我们的程序在移植过程中会变得很脆弱。我们应该小心各种各样
的陷阱。

下面是MSDN对_snprintf函数的解释:

int _snprintf( char *buffersize_t countconst char *format [, argument] ... ); Parameters
buffer
Storage location for output
count
Maximum number of characters to store
format
Format-control string
argument
Optional arguments
Libraries

All versions of the C run-time libraries.

Return Value

_snprintf returns the number of bytes stored in buffer, not counting the terminating null character. If the number of bytes required to store the data exceeds count, then count bytes of data are stored in buffer and a negative value is returned.

Remarks

The _snprintf function formats and stores count or fewer characters and values (including a terminating null character that is always appended unless count is zero or the formatted string length is greater than or equal to count characters) 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. If copying occurs between strings that overlap, the behavior is undefined.



snprintf跟_snprintf的区别

snprintf函数并不是标准c/c++中规定的函数,但是在许多编译器中,厂商提供了其实现的版本。
在gcc中,该函数名称就snprintf,而在VC中称为_snprintf。
由于不是标准函数,没有一个统一的标准来规定该函数的行为,所以导致了各厂商间的实现版本可
能会有差异。今天也的的确确看到了差异,因为这个小小的差异是我的程序无法正常的处理数据。
这个小小的差异发生在count参数。在VC中,这个count就是要写入的总字符串字符数,例如:
//VC
int main(int argc, char * argv[])
...{
    char   buff[100];
     printf("%d ",_snprintf(buff,10,"1234567890ab"));
     printf("%s",buff);
    return 0;
}

//Linxu:gcc/g++
#include <stdio.h>
int main(int argc, char * argv[])
...{
    char   buff[100];
     printf("%d ",snprintf(buff,10,"1234567890ab"));
     printf("%s",buff);
    return 0;
}
vc程序的输出是:
-1
1234567890@
gcc程序的输出是:
12
123456789
从输出结果可以知道:VC中的_snprintf的count参数表示,会向buff中写入count个字符,不包括'\0'字符,
并且不会在字符串末尾添加'\0'符,并且,如果字符串超过count,函数返回-1以标志可能导致的错误;gcc
中的snprintf函数的count参数表示,向buff中写入10个字符,包括'\0'字符,并且,返回实际的字符串长度,
例子中为12。  如果不了解这些函数在各平台间的差异,也许我们的程序在移植过程中会变得很脆弱。我们应该小心各种各样
的陷阱。

下面是MSDN对_snprintf函数的解释:

int _snprintf( char *buffersize_t countconst char *format [, argument] ... ); Parameters
buffer
Storage location for output
count
Maximum number of characters to store
format
Format-control string
argument
Optional arguments
Libraries

All versions of the C run-time libraries.

Return Value

_snprintf returns the number of bytes stored in buffer, not counting the terminating null character. If the number of bytes required to store the data exceeds count, then count bytes of data are stored in buffer and a negative value is returned.

Remarks

The _snprintf function formats and stores count or fewer characters and values (including a terminating null character that is always appended unless count is zero or the formatted string length is greater than or equal to count characters) 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. If copying occurs between strings that overlap, the behavior is undefined.

snprintf跟_snprintf的区别

snprintf函数并不是标准c/c++中规定的函数,但是在许多编译器中,厂商提供了其实现的版本。
在gcc中,该函数名称就snprintf,而在VC中称为_snprintf。
由于不是标准函数,没有一个统一的标准来规定该函数的行为,所以导致了各厂商间的实现版本可
能会有差异。今天也的的确确看到了差异,因为这个小小的差异是我的程序无法正常的处理数据。
这个小小的差异发生在count参数。在VC中,这个count就是要写入的总字符串字符数,例如:
//VC
int main(int argc, char * argv[])
...{
    char   buff[100];
     printf("%d ",_snprintf(buff,10,"1234567890ab"));
     printf("%s",buff);
    return 0;
}

//Linxu:gcc/g++
#include <stdio.h>
int main(int argc, char * argv[])
...{
    char   buff[100];
     printf("%d ",snprintf(buff,10,"1234567890ab"));
     printf("%s",buff);
    return 0;
}
vc程序的输出是:
-1
1234567890@
gcc程序的输出是:
12
123456789
从输出结果可以知道:VC中的_snprintf的count参数表示,会向buff中写入count个字符,不包括'\0'字符,
并且不会在字符串末尾添加'\0'符,并且,如果字符串超过count,函数返回-1以标志可能导致的错误;gcc
中的snprintf函数的count参数表示,向buff中写入10个字符,包括'\0'字符,并且,返回实际的字符串长度,
例子中为12。  如果不了解这些函数在各平台间的差异,也许我们的程序在移植过程中会变得很脆弱。我们应该小心各种各样
的陷阱。

下面是MSDN对_snprintf函数的解释:

int _snprintf( char *buffersize_t countconst char *format [, argument] ... ); Parameters
buffer
Storage location for output
count
Maximum number of characters to store
format
Format-control string
argument
Optional arguments
Libraries

All versions of the C run-time libraries.

Return Value

_snprintf returns the number of bytes stored in buffer, not counting the terminating null character. If the number of bytes required to store the data exceeds count, then count bytes of data are stored in buffer and a negative value is returned.

Remarks

The _snprintf function formats and stores count or fewer characters and values (including a terminating null character that is always appended unless count is zero or the formatted string length is greater than or equal to count characters) 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. If copying occurs between strings that overlap, the behavior is undefined.


snprintf函数并不是标准c/c++中规定的函数,但是在许多编译器中,厂商提供了其实现的版本。
在gcc中,该函数名称就snprintf,而在VC中称为_snprintf。
由于不是标准函数,没有一个统一的标准来规定该函数的行为,所以导致了各厂商间的实现版本可
能会有差异。今天也的的确确看到了差异,因为这个小小的差异是我的程序无法正常的处理数据。
这个小小的差异发生在count参数。在VC中,这个count就是要写入的总字符串字符数,例如:
//VC

int main(int argc, char * argv[])
...{
    char   buff[100];
     printf("%d ",_snprintf(buff,10,"1234567890ab"));
     printf("%s",buff);
    return 0;
}


//Linxu:gcc/g++

#include <stdio.h>
int main(int argc, char * argv[])
...{
    char   buff[100];
     printf("%d ",snprintf(buff,10,"1234567890ab"));
     printf("%s",buff);
    return 0;
}
vc程序的输出是:
-1
1234567890@
gcc程序的输出是:
12
123456789
从输出结果可以知道:VC中的_snprintf的count参数表示,会向buff中写入count个字符,不包括'\0'字符,
并且不会在字符串末尾添加'\0'符,并且,如果字符串超过count,函数返回-1以标志可能导致的错误;gcc
中的snprintf函数的count参数表示,向buff中写入10个字符,包括'\0'字符,并且,返回实际的字符串长度,
例子中为12。  如果不了解这些函数在各平台间的差异,也许我们的程序在移植过程中会变得很脆弱。我们应该小心各种各样
的陷阱。

下面是MSDN对_snprintf函数的解释:

int _snprintf( char *buffersize_t countconst char *format [, argument] ... ); Parameters
buffer
Storage location for output
count
Maximum number of characters to store
format
Format-control string
argument
Optional arguments
Libraries

All versions of the C run-time libraries.

Return Value

_snprintf returns the number of bytes stored in buffer, not counting the terminating null character. If the number of bytes required to store the data exceeds count, then count bytes of data are stored in buffer and a negative value is returned.

Remarks

The _snprintf function formats and stores count or fewer characters and values (including a terminating null character that is always appended unless count is zero or the formatted string length is greater than or equal to count characters) 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. If copying occurs between strings that overlap, the behavior is undefined.



snprintf跟_snprintf的区别

snprintf函数并不是标准c/c++中规定的函数,但是在许多编译器中,厂商提供了其实现的版本。
在gcc中,该函数名称就snprintf,而在VC中称为_snprintf。
由于不是标准函数,没有一个统一的标准来规定该函数的行为,所以导致了各厂商间的实现版本可
能会有差异。今天也的的确确看到了差异,因为这个小小的差异是我的程序无法正常的处理数据。
这个小小的差异发生在count参数。在VC中,这个count就是要写入的总字符串字符数,例如:
//VC
int main(int argc, char * argv[])
...{
    char   buff[100];
     printf("%d ",_snprintf(buff,10,"1234567890ab"));
     printf("%s",buff);
    return 0;
}

//Linxu:gcc/g++
#include <stdio.h>
int main(int argc, char * argv[])
...{
    char   buff[100];
     printf("%d ",snprintf(buff,10,"1234567890ab"));
     printf("%s",buff);
    return 0;
}
vc程序的输出是:
-1
1234567890@
gcc程序的输出是:
12
123456789
从输出结果可以知道:VC中的_snprintf的count参数表示,会向buff中写入count个字符,不包括'\0'字符,
并且不会在字符串末尾添加'\0'符,并且,如果字符串超过count,函数返回-1以标志可能导致的错误;gcc
中的snprintf函数的count参数表示,向buff中写入10个字符,包括'\0'字符,并且,返回实际的字符串长度,
例子中为12。  如果不了解这些函数在各平台间的差异,也许我们的程序在移植过程中会变得很脆弱。我们应该小心各种各样
的陷阱。

下面是MSDN对_snprintf函数的解释:

int _snprintf( char *buffersize_t countconst char *format [, argument] ... ); Parameters
buffer
Storage location for output
count
Maximum number of characters to store
format
Format-control string
argument
Optional arguments
Libraries

All versions of the C run-time libraries.

Return Value

_snprintf returns the number of bytes stored in buffer, not counting the terminating null character. If the number of bytes required to store the data exceeds count, then count bytes of data are stored in buffer and a negative value is returned.

Remarks

The _snprintf function formats and stores count or fewer characters and values (including a terminating null character that is always appended unless count is zero or the formatted string length is greater than or equal to count characters) 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. If copying occurs between strings that overlap, the behavior is undefined.

snprintf跟_snprintf的区别

snprintf函数并不是标准c/c++中规定的函数,但是在许多编译器中,厂商提供了其实现的版本。
在gcc中,该函数名称就snprintf,而在VC中称为_snprintf。
由于不是标准函数,没有一个统一的标准来规定该函数的行为,所以导致了各厂商间的实现版本可
能会有差异。今天也的的确确看到了差异,因为这个小小的差异是我的程序无法正常的处理数据。
这个小小的差异发生在count参数。在VC中,这个count就是要写入的总字符串字符数,例如:
//VC
int main(int argc, char * argv[])
...{
    char   buff[100];
     printf("%d ",_snprintf(buff,10,"1234567890ab"));
     printf("%s",buff);
    return 0;
}

//Linxu:gcc/g++
#include <stdio.h>
int main(int argc, char * argv[])
...{
    char   buff[100];
     printf("%d ",snprintf(buff,10,"1234567890ab"));
     printf("%s",buff);
    return 0;
}
vc程序的输出是:
-1
1234567890@
gcc程序的输出是:
12
123456789
从输出结果可以知道:VC中的_snprintf的count参数表示,会向buff中写入count个字符,不包括'\0'字符,
并且不会在字符串末尾添加'\0'符,并且,如果字符串超过count,函数返回-1以标志可能导致的错误;gcc
中的snprintf函数的count参数表示,向buff中写入10个字符,包括'\0'字符,并且,返回实际的字符串长度,
例子中为12。  如果不了解这些函数在各平台间的差异,也许我们的程序在移植过程中会变得很脆弱。我们应该小心各种各样
的陷阱。

下面是MSDN对_snprintf函数的解释:

int _snprintf( char *buffersize_t countconst char *format [, argument] ... ); Parameters
buffer
Storage location for output
count
Maximum number of characters to store
format
Format-control string
argument
Optional arguments
Libraries

All versions of the C run-time libraries.

Return Value

_snprintf returns the number of bytes stored in buffer, not counting the terminating null character. If the number of bytes required to store the data exceeds count, then count bytes of data are stored in buffer and a negative value is returned.

Remarks

The _snprintf function formats and stores count or fewer characters and values (including a terminating null character that is always appended unless count is zero or the formatted string length is greater than or equal to count characters) 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. If copying occurs between strings that overlap, the behavior is undefined.


MSDN页面分别如下:

spirntf_s:

http://msdn.microsoft.com/zh-cn/library/ce3zzk1k%28VS.80%29.aspx

_snprintf:

http://msdn.microsoft.com/zh-cn/library/2ts7cx93%28v=VS.90%29.aspx

_snprintf_s:

http://msdn.microsoft.com/zh-cn/library/f30dzcf6.aspx

三个页面都有自己的例子。。其中后2个的例子比较多内容一些。

 

为免将来页面失效:

int sprintf_s(   char *buffer,   size_t sizeOfBuffer,   const char *format [,      argument] ... 

);

template <size_t size>int sprintf_s(   char (&buffer)[size],   const char *format [,      argument] ... 

); // C++ only

int _snprintf(   char *buffer,   size_t count,   const char *format [,      argument] ... 

);

int _snprintf(   char (&buffer)[size],   size_t count,   const char *format [,      argument] ... 

); // C++ only

int _snprintf_s(   char *buffer,   size_t sizeOfBuffer,   size_t count,   const char *format [,   argument] ... 

);

int _snprintf_s(   char (&buffer)[size],   size_t count,   const char *format [,   argument] ... 

); // C++ only

 

呼呼。。内容还挺多。

 

 

这里比较引人注目的是,_snprintf_s为什么在sizeOfBuffer的基础上,还要多加一个count?

count似乎是用来控制理想的宽度的。

如果得到的字符串超过了count,于是会被截断到count的长度后面再加一个null-teminate

当然,更高优先级的应该是sizeOfBuffer,必须不超过这个大小。这个就说到点子上了。

 

如果应该输出的字符串的大小已经达到了sizeOfBuffer,那么就溢出了。溢出的情况下,sprintf_s函数把这当做一个错误,会把buffer缓冲区置为一个空字符串""。

而_snprintf_s的好处就是,有了count参数,输出的字符串就算超过缓冲区长度,仍然会有输出,输出字符串被截断到count大小,在这个大小的字符串后面加null-teminate。

当然,如果count被设置成和sizeOfBuffer同样大,或者不合理的更大,那么这个count参数就失去了意义。

这时候,如果输出字符串将要达到或者超过sizeOfBuffer,一样导致一个错误,输出缓冲区被置为空字符串。

因此,如果希望缓冲区被尽量利用,可以把count参数置为_TRUNCATE,这样的情况下,实际上效果相当于是将count设置为sizeOfBuffer - 1。

 

 

至于C语言环境下,sprintf_s与_snprintf的对比:

注意到,_snprintf的参数用的是count,而sprintf_s的参数用的是sizeOfBuffer。这很能说明问题。

看下对_snprintf的说明:

Let len be the length of the formatted data string (not including the terminating null). len and count are in bytes for _snprintf, wide characters for _snwprintf.

If len < count, then len characters are stored in buffer, a null-terminator is appended, and len is returned.

If len = count, then len characters are stored in buffer, no null-terminator is appended, and len is returned.

If len > count, then count characters are stored in buffer, no null-terminator is appended, and a negative value is returned.

 

也就是说,_snprintf的count参数明明白白的就是一个count。

如果输出字符串刚好达到count,由于期待的最大长度就是count,那么输出字符串肯定要完整,不能截断。

但是假如字符串缓冲区的大小其实就是count,这怎么办?MS VCRT的设计者认为,在这种情况下应该把输出字符串的长度告知调用者,让调用者来决定是否自己添加null-teminate。

换句话说,调用_snprintf时要注意了,必须检查_snprintf的返回值,如果返回值不是正数,那么还得注意你的字符串缓冲区并不是null-teminate结尾的。

 

总结来说,sprintf_s在缓冲区不够大时会失败,失败时缓冲区中是一个空字符串。

_snprintf不会失败,但是必须注意如果缓冲区不够大,缓冲区的内容将不是null-teminate的,必须自己注意字符串的结束。

_snprintf_s结合了2者的优点,只要count参数设置合理,函数就不会因缓冲区不够而失败。

 

但是观察_snprintf_s的说明,有一个很有趣的内容。

这3族函数中,有失败情况的2个函数sprintf_s和_snprintf_s中,(再次强调,我这里的失败的意思是,调用后缓冲区里是一个空字符串),_set_invalid_parameter_handler设置的错误处理器,在失败的情况下会被调用。

 

而截断的情况下,错误处理器并不会被调用。

 

VC的库开发者总是提供一些怪怪的东西。无论如何,让代码更加安全总是符合大家的总体期望的。

另外补充一下,查阅这些字符串安全函数的资料的时候要注意,

对微软来说,凡是限制字符串复制长度的函数,这些设计者仍然认为是不安全的,因为逻辑上来说,

这些长度参数只是限制了源字符串被复制的长度,而不是目标缓冲区的长度。

也就是说,微软的这些设计者认为,安全的方式其实是依赖C++的机制,辨认出目标缓冲区的真正大小,以此实现安全的复制。




snprintf跟_snprintf的区别

snprintf函数并不是标准c/c++中规定的函数,但是在许多编译器中,厂商提供了其实现的版本。
在gcc中,该函数名称就snprintf,而在VC中称为_snprintf。
由于不是标准函数,没有一个统一的标准来规定该函数的行为,所以导致了各厂商间的实现版本可
能会有差异。今天也的的确确看到了差异,因为这个小小的差异是我的程序无法正常的处理数据。
这个小小的差异发生在count参数。在VC中,这个count就是要写入的总字符串字符数,例如:
//VC
int main(int argc, char * argv[])
...{
    char   buff[100];
     printf("%d ",_snprintf(buff,10,"1234567890ab"));
     printf("%s",buff);
    return 0;
}

//Linxu:gcc/g++
#include <stdio.h>
int main(int argc, char * argv[])
...{
    char   buff[100];
     printf("%d ",snprintf(buff,10,"1234567890ab"));
     printf("%s",buff);
    return 0;
}
vc程序的输出是:
-1
1234567890@
gcc程序的输出是:
12
123456789
从输出结果可以知道:VC中的_snprintf的count参数表示,会向buff中写入count个字符,不包括'\0'字符,
并且不会在字符串末尾添加'\0'符,并且,如果字符串超过count,函数返回-1以标志可能导致的错误;gcc
中的snprintf函数的count参数表示,向buff中写入10个字符,包括'\0'字符,并且,返回实际的字符串长度,
例子中为12。  如果不了解这些函数在各平台间的差异,也许我们的程序在移植过程中会变得很脆弱。我们应该小心各种各样
的陷阱。

下面是MSDN对_snprintf函数的解释:

int _snprintf( char *buffersize_t countconst char *format [, argument] ... ); Parameters
buffer
Storage location for output
count
Maximum number of characters to store
format
Format-control string
argument
Optional arguments
Libraries

All versions of the C run-time libraries.

Return Value

_snprintf returns the number of bytes stored in buffer, not counting the terminating null character. If the number of bytes required to store the data exceeds count, then count bytes of data are stored in buffer and a negative value is returned.

Remarks

The _snprintf function formats and stores count or fewer characters and values (including a terminating null character that is always appended unless count is zero or the formatted string length is greater than or equal to count characters) 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. If copying occurs between strings that overlap, the behavior is undefined.

snprintf跟_snprintf的区别

snprintf函数并不是标准c/c++中规定的函数,但是在许多编译器中,厂商提供了其实现的版本。
在gcc中,该函数名称就snprintf,而在VC中称为_snprintf。
由于不是标准函数,没有一个统一的标准来规定该函数的行为,所以导致了各厂商间的实现版本可
能会有差异。今天也的的确确看到了差异,因为这个小小的差异是我的程序无法正常的处理数据。
这个小小的差异发生在count参数。在VC中,这个count就是要写入的总字符串字符数,例如:
//VC
int main(int argc, char * argv[])
...{
    char   buff[100];
     printf("%d ",_snprintf(buff,10,"1234567890ab"));
     printf("%s",buff);
    return 0;
}

//Linxu:gcc/g++
#include <stdio.h>
int main(int argc, char * argv[])
...{
    char   buff[100];
     printf("%d ",snprintf(buff,10,"1234567890ab"));
     printf("%s",buff);
    return 0;
}
vc程序的输出是:
-1
1234567890@
gcc程序的输出是:
12
123456789
从输出结果可以知道:VC中的_snprintf的count参数表示,会向buff中写入count个字符,不包括'\0'字符,
并且不会在字符串末尾添加'\0'符,并且,如果字符串超过count,函数返回-1以标志可能导致的错误;gcc
中的snprintf函数的count参数表示,向buff中写入10个字符,包括'\0'字符,并且,返回实际的字符串长度,
例子中为12。  如果不了解这些函数在各平台间的差异,也许我们的程序在移植过程中会变得很脆弱。我们应该小心各种各样
的陷阱。

下面是MSDN对_snprintf函数的解释:

int _snprintf( char *buffersize_t countconst char *format [, argument] ... ); Parameters
buffer
Storage location for output
count
Maximum number of characters to store
format
Format-control string
argument
Optional arguments
Libraries

All versions of the C run-time libraries.

Return Value

_snprintf returns the number of bytes stored in buffer, not counting the terminating null character. If the number of bytes required to store the data exceeds count, then count bytes of data are stored in buffer and a negative value is returned.

Remarks

The _snprintf function formats and stores count or fewer characters and values (including a terminating null character that is always appended unless count is zero or the formatted string length is greater than or equal to count characters) 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. If copying occurs between strings that overlap, the behavior is undefined.


snprintf函数并不是标准c/c++中规定的函数,但是在许多编译器中,厂商提供了其实现的版本。
在gcc中,该函数名称就snprintf,而在VC中称为_snprintf。
由于不是标准函数,没有一个统一的标准来规定该函数的行为,所以导致了各厂商间的实现版本可
能会有差异。今天也的的确确看到了差异,因为这个小小的差异是我的程序无法正常的处理数据。
这个小小的差异发生在count参数。在VC中,这个count就是要写入的总字符串字符数,例如:
//VC

int main(int argc, char * argv[])
...{
    char   buff[100];
     printf("%d ",_snprintf(buff,10,"1234567890ab"));
     printf("%s",buff);
    return 0;
}


//Linxu:gcc/g++

#include <stdio.h>
int main(int argc, char * argv[])
...{
    char   buff[100];
     printf("%d ",snprintf(buff,10,"1234567890ab"));
     printf("%s",buff);
    return 0;
}
vc程序的输出是:
-1
1234567890@
gcc程序的输出是:
12
123456789
从输出结果可以知道:VC中的_snprintf的count参数表示,会向buff中写入count个字符,不包括'\0'字符,
并且不会在字符串末尾添加'\0'符,并且,如果字符串超过count,函数返回-1以标志可能导致的错误;gcc
中的snprintf函数的count参数表示,向buff中写入10个字符,包括'\0'字符,并且,返回实际的字符串长度,
例子中为12。  如果不了解这些函数在各平台间的差异,也许我们的程序在移植过程中会变得很脆弱。我们应该小心各种各样
的陷阱。

下面是MSDN对_snprintf函数的解释:

int _snprintf( char *buffersize_t countconst char *format [, argument] ... ); Parameters
buffer
Storage location for output
count
Maximum number of characters to store
format
Format-control string
argument
Optional arguments
Libraries

All versions of the C run-time libraries.

Return Value

_snprintf returns the number of bytes stored in buffer, not counting the terminating null character. If the number of bytes required to store the data exceeds count, then count bytes of data are stored in buffer and a negative value is returned.

Remarks

The _snprintf function formats and stores count or fewer characters and values (including a terminating null character that is always appended unless count is zero or the formatted string length is greater than or equal to count characters) 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. If copying occurs between strings that overlap, the behavior is undefined.



snprintf跟_snprintf的区别

snprintf函数并不是标准c/c++中规定的函数,但是在许多编译器中,厂商提供了其实现的版本。
在gcc中,该函数名称就snprintf,而在VC中称为_snprintf。
由于不是标准函数,没有一个统一的标准来规定该函数的行为,所以导致了各厂商间的实现版本可
能会有差异。今天也的的确确看到了差异,因为这个小小的差异是我的程序无法正常的处理数据。
这个小小的差异发生在count参数。在VC中,这个count就是要写入的总字符串字符数,例如:
//VC
int main(int argc, char * argv[])
...{
    char   buff[100];
     printf("%d ",_snprintf(buff,10,"1234567890ab"));
     printf("%s",buff);
    return 0;
}

//Linxu:gcc/g++
#include <stdio.h>
int main(int argc, char * argv[])
...{
    char   buff[100];
     printf("%d ",snprintf(buff,10,"1234567890ab"));
     printf("%s",buff);
    return 0;
}
vc程序的输出是:
-1
1234567890@
gcc程序的输出是:
12
123456789
从输出结果可以知道:VC中的_snprintf的count参数表示,会向buff中写入count个字符,不包括'\0'字符,
并且不会在字符串末尾添加'\0'符,并且,如果字符串超过count,函数返回-1以标志可能导致的错误;gcc
中的snprintf函数的count参数表示,向buff中写入10个字符,包括'\0'字符,并且,返回实际的字符串长度,
例子中为12。  如果不了解这些函数在各平台间的差异,也许我们的程序在移植过程中会变得很脆弱。我们应该小心各种各样
的陷阱。

下面是MSDN对_snprintf函数的解释:

int _snprintf( char *buffersize_t countconst char *format [, argument] ... ); Parameters
buffer
Storage location for output
count
Maximum number of characters to store
format
Format-control string
argument
Optional arguments
Libraries

All versions of the C run-time libraries.

Return Value

_snprintf returns the number of bytes stored in buffer, not counting the terminating null character. If the number of bytes required to store the data exceeds count, then count bytes of data are stored in buffer and a negative value is returned.

Remarks

The _snprintf function formats and stores count or fewer characters and values (including a terminating null character that is always appended unless count is zero or the formatted string length is greater than or equal to count characters) 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. If copying occurs between strings that overlap, the behavior is undefined.

snprintf跟_snprintf的区别

snprintf函数并不是标准c/c++中规定的函数,但是在许多编译器中,厂商提供了其实现的版本。
在gcc中,该函数名称就snprintf,而在VC中称为_snprintf。
由于不是标准函数,没有一个统一的标准来规定该函数的行为,所以导致了各厂商间的实现版本可
能会有差异。今天也的的确确看到了差异,因为这个小小的差异是我的程序无法正常的处理数据。
这个小小的差异发生在count参数。在VC中,这个count就是要写入的总字符串字符数,例如:
//VC
int main(int argc, char * argv[])
...{
    char   buff[100];
     printf("%d ",_snprintf(buff,10,"1234567890ab"));
     printf("%s",buff);
    return 0;
}

//Linxu:gcc/g++
#include <stdio.h>
int main(int argc, char * argv[])
...{
    char   buff[100];
     printf("%d ",snprintf(buff,10,"1234567890ab"));
     printf("%s",buff);
    return 0;
}
vc程序的输出是:
-1
1234567890@
gcc程序的输出是:
12
123456789
从输出结果可以知道:VC中的_snprintf的count参数表示,会向buff中写入count个字符,不包括'\0'字符,
并且不会在字符串末尾添加'\0'符,并且,如果字符串超过count,函数返回-1以标志可能导致的错误;gcc
中的snprintf函数的count参数表示,向buff中写入10个字符,包括'\0'字符,并且,返回实际的字符串长度,
例子中为12。  如果不了解这些函数在各平台间的差异,也许我们的程序在移植过程中会变得很脆弱。我们应该小心各种各样
的陷阱。

下面是MSDN对_snprintf函数的解释:

int _snprintf( char *buffersize_t countconst char *format [, argument] ... ); Parameters
buffer
Storage location for output
count
Maximum number of characters to store
format
Format-control string
argument
Optional arguments
Libraries

All versions of the C run-time libraries.

Return Value

_snprintf returns the number of bytes stored in buffer, not counting the terminating null character. If the number of bytes required to store the data exceeds count, then count bytes of data are stored in buffer and a negative value is returned.

Remarks

The _snprintf function formats and stores count or fewer characters and values (including a terminating null character that is always appended unless count is zero or the formatted string length is greater than or equal to count characters) 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. If copying occurs between strings that overlap, the behavior is undefined.


snprintf函数并不是标准c/c++中规定的函数,但是在许多编译器中,厂商提供了其实现的版本。
在gcc中,该函数名称就snprintf,而在VC中称为_snprintf。
由于不是标准函数,没有一个统一的标准来规定该函数的行为,所以导致了各厂商间的实现版本可
能会有差异。今天也的的确确看到了差异,因为这个小小的差异是我的程序无法正常的处理数据。
这个小小的差异发生在count参数。在VC中,这个count就是要写入的总字符串字符数,例如:
//VC

int main(int argc, char * argv[])
...{
    char   buff[100];
     printf("%d ",_snprintf(buff,10,"1234567890ab"));
     printf("%s",buff);
    return 0;
}


//Linxu:gcc/g++

#include <stdio.h>
int main(int argc, char * argv[])
...{
    char   buff[100];
     printf("%d ",snprintf(buff,10,"1234567890ab"));
     printf("%s",buff);
    return 0;
}
vc程序的输出是:
-1
1234567890@
gcc程序的输出是:
12
123456789
从输出结果可以知道:VC中的_snprintf的count参数表示,会向buff中写入count个字符,不包括'\0'字符,
并且不会在字符串末尾添加'\0'符,并且,如果字符串超过count,函数返回-1以标志可能导致的错误;gcc
中的snprintf函数的count参数表示,向buff中写入10个字符,包括'\0'字符,并且,返回实际的字符串长度,
例子中为12。  如果不了解这些函数在各平台间的差异,也许我们的程序在移植过程中会变得很脆弱。我们应该小心各种各样
的陷阱。

下面是MSDN对_snprintf函数的解释:

int _snprintf( char *buffersize_t countconst char *format [, argument] ... ); Parameters
buffer
Storage location for output
count
Maximum number of characters to store
format
Format-control string
argument
Optional arguments
Libraries

All versions of the C run-time libraries.

Return Value

_snprintf returns the number of bytes stored in buffer, not counting the terminating null character. If the number of bytes required to store the data exceeds count, then count bytes of data are stored in buffer and a negative value is returned.

Remarks

The _snprintf function formats and stores count or fewer characters and values (including a terminating null character that is always appended unless count is zero or the formatted string length is greater than or equal to count characters) 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. If copying occurs between strings that overlap, the behavior is undefined.



snprintf跟_snprintf的区别

snprintf函数并不是标准c/c++中规定的函数,但是在许多编译器中,厂商提供了其实现的版本。
在gcc中,该函数名称就snprintf,而在VC中称为_snprintf。
由于不是标准函数,没有一个统一的标准来规定该函数的行为,所以导致了各厂商间的实现版本可
能会有差异。今天也的的确确看到了差异,因为这个小小的差异是我的程序无法正常的处理数据。
这个小小的差异发生在count参数。在VC中,这个count就是要写入的总字符串字符数,例如:
//VC
int main(int argc, char * argv[])
...{
    char   buff[100];
     printf("%d ",_snprintf(buff,10,"1234567890ab"));
     printf("%s",buff);
    return 0;
}

//Linxu:gcc/g++
#include <stdio.h>
int main(int argc, char * argv[])
...{
    char   buff[100];
     printf("%d ",snprintf(buff,10,"1234567890ab"));
     printf("%s",buff);
    return 0;
}
vc程序的输出是:
-1
1234567890@
gcc程序的输出是:
12
123456789
从输出结果可以知道:VC中的_snprintf的count参数表示,会向buff中写入count个字符,不包括'\0'字符,
并且不会在字符串末尾添加'\0'符,并且,如果字符串超过count,函数返回-1以标志可能导致的错误;gcc
中的snprintf函数的count参数表示,向buff中写入10个字符,包括'\0'字符,并且,返回实际的字符串长度,
例子中为12。  如果不了解这些函数在各平台间的差异,也许我们的程序在移植过程中会变得很脆弱。我们应该小心各种各样
的陷阱。

下面是MSDN对_snprintf函数的解释:

int _snprintf( char *buffersize_t countconst char *format [, argument] ... ); Parameters
buffer
Storage location for output
count
Maximum number of characters to store
format
Format-control string
argument
Optional arguments
Libraries

All versions of the C run-time libraries.

Return Value

_snprintf returns the number of bytes stored in buffer, not counting the terminating null character. If the number of bytes required to store the data exceeds count, then count bytes of data are stored in buffer and a negative value is returned.

Remarks

The _snprintf function formats and stores count or fewer characters and values (including a terminating null character that is always appended unless count is zero or the formatted string length is greater than or equal to count characters) 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. If copying occurs between strings that overlap, the behavior is undefined.

snprintf跟_snprintf的区别

snprintf函数并不是标准c/c++中规定的函数,但是在许多编译器中,厂商提供了其实现的版本。
在gcc中,该函数名称就snprintf,而在VC中称为_snprintf。
由于不是标准函数,没有一个统一的标准来规定该函数的行为,所以导致了各厂商间的实现版本可
能会有差异。今天也的的确确看到了差异,因为这个小小的差异是我的程序无法正常的处理数据。
这个小小的差异发生在count参数。在VC中,这个count就是要写入的总字符串字符数,例如:
//VC
int main(int argc, char * argv[])
...{
    char   buff[100];
     printf("%d ",_snprintf(buff,10,"1234567890ab"));
     printf("%s",buff);
    return 0;
}

//Linxu:gcc/g++
#include <stdio.h>
int main(int argc, char * argv[])
...{
    char   buff[100];
     printf("%d ",snprintf(buff,10,"1234567890ab"));
     printf("%s",buff);
    return 0;
}
vc程序的输出是:
-1
1234567890@
gcc程序的输出是:
12
123456789
从输出结果可以知道:VC中的_snprintf的count参数表示,会向buff中写入count个字符,不包括'\0'字符,
并且不会在字符串末尾添加'\0'符,并且,如果字符串超过count,函数返回-1以标志可能导致的错误;gcc
中的snprintf函数的count参数表示,向buff中写入10个字符,包括'\0'字符,并且,返回实际的字符串长度,
例子中为12。  如果不了解这些函数在各平台间的差异,也许我们的程序在移植过程中会变得很脆弱。我们应该小心各种各样
的陷阱。

下面是MSDN对_snprintf函数的解释:

int _snprintf( char *buffersize_t countconst char *format [, argument] ... ); Parameters
buffer
Storage location for output
count
Maximum number of characters to store
format
Format-control string
argument
Optional arguments
Libraries

All versions of the C run-time libraries.

Return Value

_snprintf returns the number of bytes stored in buffer, not counting the terminating null character. If the number of bytes required to store the data exceeds count, then count bytes of data are stored in buffer and a negative value is returned.

Remarks

The _snprintf function formats and stores count or fewer characters and values (including a terminating null character that is always appended unless count is zero or the formatted string length is greater than or equal to count characters) 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. If copying occurs between strings that overlap, the behavior is undefined.


snprintf函数并不是标准c/c++中规定的函数,但是在许多编译器中,厂商提供了其实现的版本。
在gcc中,该函数名称就snprintf,而在VC中称为_snprintf。
由于不是标准函数,没有一个统一的标准来规定该函数的行为,所以导致了各厂商间的实现版本可
能会有差异。今天也的的确确看到了差异,因为这个小小的差异是我的程序无法正常的处理数据。
这个小小的差异发生在count参数。在VC中,这个count就是要写入的总字符串字符数,例如:
//VC

int main(int argc, char * argv[])
...{
    char   buff[100];
     printf("%d ",_snprintf(buff,10,"1234567890ab"));
     printf("%s",buff);
    return 0;
}


//Linxu:gcc/g++

#include <stdio.h>
int main(int argc, char * argv[])
...{
    char   buff[100];
     printf("%d ",snprintf(buff,10,"1234567890ab"));
     printf("%s",buff);
    return 0;
}
vc程序的输出是:
-1
1234567890@
gcc程序的输出是:
12
123456789
从输出结果可以知道:VC中的_snprintf的count参数表示,会向buff中写入count个字符,不包括'\0'字符,
并且不会在字符串末尾添加'\0'符,并且,如果字符串超过count,函数返回-1以标志可能导致的错误;gcc
中的snprintf函数的count参数表示,向buff中写入10个字符,包括'\0'字符,并且,返回实际的字符串长度,
例子中为12。  如果不了解这些函数在各平台间的差异,也许我们的程序在移植过程中会变得很脆弱。我们应该小心各种各样
的陷阱。

下面是MSDN对_snprintf函数的解释:

int _snprintf( char *buffersize_t countconst char *format [, argument] ... ); Parameters
buffer
Storage location for output
count
Maximum number of characters to store
format
Format-control string
argument
Optional arguments
Libraries

All versions of the C run-time libraries.

Return Value

_snprintf returns the number of bytes stored in buffer, not counting the terminating null character. If the number of bytes required to store the data exceeds count, then count bytes of data are stored in buffer and a negative value is returned.

Remarks

The _snprintf function formats and stores count or fewer characters and values (including a terminating null character that is always appended unless count is zero or the formatted string length is greater than or equal to count characters) 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. If copying occurs between strings that overlap, the behavior is undefined.



snprintf跟_snprintf的区别

snprintf函数并不是标准c/c++中规定的函数,但是在许多编译器中,厂商提供了其实现的版本。
在gcc中,该函数名称就snprintf,而在VC中称为_snprintf。
由于不是标准函数,没有一个统一的标准来规定该函数的行为,所以导致了各厂商间的实现版本可
能会有差异。今天也的的确确看到了差异,因为这个小小的差异是我的程序无法正常的处理数据。
这个小小的差异发生在count参数。在VC中,这个count就是要写入的总字符串字符数,例如:
//VC
int main(int argc, char * argv[])
...{
    char   buff[100];
     printf("%d ",_snprintf(buff,10,"1234567890ab"));
     printf("%s",buff);
    return 0;
}

//Linxu:gcc/g++
#include <stdio.h>
int main(int argc, char * argv[])
...{
    char   buff[100];
     printf("%d ",snprintf(buff,10,"1234567890ab"));
     printf("%s",buff);
    return 0;
}
vc程序的输出是:
-1
1234567890@
gcc程序的输出是:
12
123456789
从输出结果可以知道:VC中的_snprintf的count参数表示,会向buff中写入count个字符,不包括'\0'字符,
并且不会在字符串末尾添加'\0'符,并且,如果字符串超过count,函数返回-1以标志可能导致的错误;gcc
中的snprintf函数的count参数表示,向buff中写入10个字符,包括'\0'字符,并且,返回实际的字符串长度,
例子中为12。  如果不了解这些函数在各平台间的差异,也许我们的程序在移植过程中会变得很脆弱。我们应该小心各种各样
的陷阱。

下面是MSDN对_snprintf函数的解释:

int _snprintf( char *buffersize_t countconst char *format [, argument] ... ); Parameters
buffer
Storage location for output
count
Maximum number of characters to store
format
Format-control string
argument
Optional arguments
Libraries

All versions of the C run-time libraries.

Return Value

_snprintf returns the number of bytes stored in buffer, not counting the terminating null character. If the number of bytes required to store the data exceeds count, then count bytes of data are stored in buffer and a negative value is returned.

Remarks

The _snprintf function formats and stores count or fewer characters and values (including a terminating null character that is always appended unless count is zero or the formatted string length is greater than or equal to count characters) 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. If copying occurs between strings that overlap, the behavior is undefined.

snprintf跟_snprintf的区别

snprintf函数并不是标准c/c++中规定的函数,但是在许多编译器中,厂商提供了其实现的版本。
在gcc中,该函数名称就snprintf,而在VC中称为_snprintf。
由于不是标准函数,没有一个统一的标准来规定该函数的行为,所以导致了各厂商间的实现版本可
能会有差异。今天也的的确确看到了差异,因为这个小小的差异是我的程序无法正常的处理数据。
这个小小的差异发生在count参数。在VC中,这个count就是要写入的总字符串字符数,例如:
//VC
int main(int argc, char * argv[])
...{
    char   buff[100];
     printf("%d ",_snprintf(buff,10,"1234567890ab"));
     printf("%s",buff);
    return 0;
}

//Linxu:gcc/g++
#include <stdio.h>
int main(int argc, char * argv[])
...{
    char   buff[100];
     printf("%d ",snprintf(buff,10,"1234567890ab"));
     printf("%s",buff);
    return 0;
}
vc程序的输出是:
-1
1234567890@
gcc程序的输出是:
12
123456789
从输出结果可以知道:VC中的_snprintf的count参数表示,会向buff中写入count个字符,不包括'\0'字符,
并且不会在字符串末尾添加'\0'符,并且,如果字符串超过count,函数返回-1以标志可能导致的错误;gcc
中的snprintf函数的count参数表示,向buff中写入10个字符,包括'\0'字符,并且,返回实际的字符串长度,
例子中为12。  如果不了解这些函数在各平台间的差异,也许我们的程序在移植过程中会变得很脆弱。我们应该小心各种各样
的陷阱。

下面是MSDN对_snprintf函数的解释:

int _snprintf( char *buffersize_t countconst char *format [, argument] ... ); Parameters
buffer
Storage location for output
count
Maximum number of characters to store
format
Format-control string
argument
Optional arguments
Libraries

All versions of the C run-time libraries.

Return Value

_snprintf returns the number of bytes stored in buffer, not counting the terminating null character. If the number of bytes required to store the data exceeds count, then count bytes of data are stored in buffer and a negative value is returned.

Remarks

The _snprintf function formats and stores count or fewer characters and values (including a terminating null character that is always appended unless count is zero or the formatted string length is greater than or equal to count characters) 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. If copying occurs between strings that overlap, the behavior is undefined.


MSDN页面分别如下:

spirntf_s:

http://msdn.microsoft.com/zh-cn/library/ce3zzk1k%28VS.80%29.aspx

_snprintf:

http://msdn.microsoft.com/zh-cn/library/2ts7cx93%28v=VS.90%29.aspx

_snprintf_s:

http://msdn.microsoft.com/zh-cn/library/f30dzcf6.aspx

三个页面都有自己的例子。。其中后2个的例子比较多内容一些。

 

为免将来页面失效:

int sprintf_s(   char *buffer,   size_t sizeOfBuffer,   const char *format [,      argument] ... 

);

template <size_t size>int sprintf_s(   char (&buffer)[size],   const char *format [,      argument] ... 

); // C++ only

int _snprintf(   char *buffer,   size_t count,   const char *format [,      argument] ... 

);

int _snprintf(   char (&buffer)[size],   size_t count,   const char *format [,      argument] ... 

); // C++ only

int _snprintf_s(   char *buffer,   size_t sizeOfBuffer,   size_t count,   const char *format [,   argument] ... 

);

int _snprintf_s(   char (&buffer)[size],   size_t count,   const char *format [,   argument] ... 

); // C++ only

 

呼呼。。内容还挺多。

 

 

这里比较引人注目的是,_snprintf_s为什么在sizeOfBuffer的基础上,还要多加一个count?

count似乎是用来控制理想的宽度的。

如果得到的字符串超过了count,于是会被截断到count的长度后面再加一个null-teminate

当然,更高优先级的应该是sizeOfBuffer,必须不超过这个大小。这个就说到点子上了。

 

如果应该输出的字符串的大小已经达到了sizeOfBuffer,那么就溢出了。溢出的情况下,sprintf_s函数把这当做一个错误,会把buffer缓冲区置为一个空字符串""。

而_snprintf_s的好处就是,有了count参数,输出的字符串就算超过缓冲区长度,仍然会有输出,输出字符串被截断到count大小,在这个大小的字符串后面加null-teminate。

当然,如果count被设置成和sizeOfBuffer同样大,或者不合理的更大,那么这个count参数就失去了意义。

这时候,如果输出字符串将要达到或者超过sizeOfBuffer,一样导致一个错误,输出缓冲区被置为空字符串。

因此,如果希望缓冲区被尽量利用,可以把count参数置为_TRUNCATE,这样的情况下,实际上效果相当于是将count设置为sizeOfBuffer - 1。

 

 

至于C语言环境下,sprintf_s与_snprintf的对比:

注意到,_snprintf的参数用的是count,而sprintf_s的参数用的是sizeOfBuffer。这很能说明问题。

看下对_snprintf的说明:

Let len be the length of the formatted data string (not including the terminating null). len and count are in bytes for _snprintf, wide characters for _snwprintf.

If len < count, then len characters are stored in buffer, a null-terminator is appended, and len is returned.

If len = count, then len characters are stored in buffer, no null-terminator is appended, and len is returned.

If len > count, then count characters are stored in buffer, no null-terminator is appended, and a negative value is returned.

 

也就是说,_snprintf的count参数明明白白的就是一个count。

如果输出字符串刚好达到count,由于期待的最大长度就是count,那么输出字符串肯定要完整,不能截断。

但是假如字符串缓冲区的大小其实就是count,这怎么办?MS VCRT的设计者认为,在这种情况下应该把输出字符串的长度告知调用者,让调用者来决定是否自己添加null-teminate。

换句话说,调用_snprintf时要注意了,必须检查_snprintf的返回值,如果返回值不是正数,那么还得注意你的字符串缓冲区并不是null-teminate结尾的。

 

总结来说,sprintf_s在缓冲区不够大时会失败,失败时缓冲区中是一个空字符串。

_snprintf不会失败,但是必须注意如果缓冲区不够大,缓冲区的内容将不是null-teminate的,必须自己注意字符串的结束。

_snprintf_s结合了2者的优点,只要count参数设置合理,函数就不会因缓冲区不够而失败。

 

但是观察_snprintf_s的说明,有一个很有趣的内容。

这3族函数中,有失败情况的2个函数sprintf_s和_snprintf_s中,(再次强调,我这里的失败的意思是,调用后缓冲区里是一个空字符串),_set_invalid_parameter_handler设置的错误处理器,在失败的情况下会被调用。

 

而截断的情况下,错误处理器并不会被调用。

 

VC的库开发者总是提供一些怪怪的东西。无论如何,让代码更加安全总是符合大家的总体期望的。

另外补充一下,查阅这些字符串安全函数的资料的时候要注意,

对微软来说,凡是限制字符串复制长度的函数,这些设计者仍然认为是不安全的,因为逻辑上来说,

这些长度参数只是限制了源字符串被复制的长度,而不是目标缓冲区的长度。

也就是说,微软的这些设计者认为,安全的方式其实是依赖C++的机制,辨认出目标缓冲区的真正大小,以此实现安全的复制。




snprintf跟_snprintf的区别

snprintf函数并不是标准c/c++中规定的函数,但是在许多编译器中,厂商提供了其实现的版本。
在gcc中,该函数名称就snprintf,而在VC中称为_snprintf。
由于不是标准函数,没有一个统一的标准来规定该函数的行为,所以导致了各厂商间的实现版本可
能会有差异。今天也的的确确看到了差异,因为这个小小的差异是我的程序无法正常的处理数据。
这个小小的差异发生在count参数。在VC中,这个count就是要写入的总字符串字符数,例如:
//VC
int main(int argc, char * argv[])
...{
    char   buff[100];
     printf("%d ",_snprintf(buff,10,"1234567890ab"));
     printf("%s",buff);
    return 0;
}

//Linxu:gcc/g++
#include <stdio.h>
int main(int argc, char * argv[])
...{
    char   buff[100];
     printf("%d ",snprintf(buff,10,"1234567890ab"));
     printf("%s",buff);
    return 0;
}
vc程序的输出是:
-1
1234567890@
gcc程序的输出是:
12
123456789
从输出结果可以知道:VC中的_snprintf的count参数表示,会向buff中写入count个字符,不包括'\0'字符,
并且不会在字符串末尾添加'\0'符,并且,如果字符串超过count,函数返回-1以标志可能导致的错误;gcc
中的snprintf函数的count参数表示,向buff中写入10个字符,包括'\0'字符,并且,返回实际的字符串长度,
例子中为12。  如果不了解这些函数在各平台间的差异,也许我们的程序在移植过程中会变得很脆弱。我们应该小心各种各样
的陷阱。

下面是MSDN对_snprintf函数的解释:

int _snprintf( char *buffersize_t countconst char *format [, argument] ... ); Parameters
buffer
Storage location for output
count
Maximum number of characters to store
format
Format-control string
argument
Optional arguments
Libraries

All versions of the C run-time libraries.

Return Value

_snprintf returns the number of bytes stored in buffer, not counting the terminating null character. If the number of bytes required to store the data exceeds count, then count bytes of data are stored in buffer and a negative value is returned.

Remarks

The _snprintf function formats and stores count or fewer characters and values (including a terminating null character that is always appended unless count is zero or the formatted string length is greater than or equal to count characters) 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. If copying occurs between strings that overlap, the behavior is undefined.

snprintf跟_snprintf的区别

snprintf函数并不是标准c/c++中规定的函数,但是在许多编译器中,厂商提供了其实现的版本。
在gcc中,该函数名称就snprintf,而在VC中称为_snprintf。
由于不是标准函数,没有一个统一的标准来规定该函数的行为,所以导致了各厂商间的实现版本可
能会有差异。今天也的的确确看到了差异,因为这个小小的差异是我的程序无法正常的处理数据。
这个小小的差异发生在count参数。在VC中,这个count就是要写入的总字符串字符数,例如:
//VC
int main(int argc, char * argv[])
...{
    char   buff[100];
     printf("%d ",_snprintf(buff,10,"1234567890ab"));
     printf("%s",buff);
    return 0;
}

//Linxu:gcc/g++
#include <stdio.h>
int main(int argc, char * argv[])
...{
    char   buff[100];
     printf("%d ",snprintf(buff,10,"1234567890ab"));
     printf("%s",buff);
    return 0;
}
vc程序的输出是:
-1
1234567890@
gcc程序的输出是:
12
123456789
从输出结果可以知道:VC中的_snprintf的count参数表示,会向buff中写入count个字符,不包括'\0'字符,
并且不会在字符串末尾添加'\0'符,并且,如果字符串超过count,函数返回-1以标志可能导致的错误;gcc
中的snprintf函数的count参数表示,向buff中写入10个字符,包括'\0'字符,并且,返回实际的字符串长度,
例子中为12。  如果不了解这些函数在各平台间的差异,也许我们的程序在移植过程中会变得很脆弱。我们应该小心各种各样
的陷阱。

下面是MSDN对_snprintf函数的解释:

int _snprintf( char *buffersize_t countconst char *format [, argument] ... ); Parameters
buffer
Storage location for output
count
Maximum number of characters to store
format
Format-control string
argument
Optional arguments
Libraries

All versions of the C run-time libraries.

Return Value

_snprintf returns the number of bytes stored in buffer, not counting the terminating null character. If the number of bytes required to store the data exceeds count, then count bytes of data are stored in buffer and a negative value is returned.

Remarks

The _snprintf function formats and stores count or fewer characters and values (including a terminating null character that is always appended unless count is zero or the formatted string length is greater than or equal to count characters) 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. If copying occurs between strings that overlap, the behavior is undefined.


snprintf函数并不是标准c/c++中规定的函数,但是在许多编译器中,厂商提供了其实现的版本。
在gcc中,该函数名称就snprintf,而在VC中称为_snprintf。
由于不是标准函数,没有一个统一的标准来规定该函数的行为,所以导致了各厂商间的实现版本可
能会有差异。今天也的的确确看到了差异,因为这个小小的差异是我的程序无法正常的处理数据。
这个小小的差异发生在count参数。在VC中,这个count就是要写入的总字符串字符数,例如:
//VC

int main(int argc, char * argv[])
...{
    char   buff[100];
     printf("%d ",_snprintf(buff,10,"1234567890ab"));
     printf("%s",buff);
    return 0;
}


//Linxu:gcc/g++

#include <stdio.h>
int main(int argc, char * argv[])
...{
    char   buff[100];
     printf("%d ",snprintf(buff,10,"1234567890ab"));
     printf("%s",buff);
    return 0;
}
vc程序的输出是:
-1
1234567890@
gcc程序的输出是:
12
123456789
从输出结果可以知道:VC中的_snprintf的count参数表示,会向buff中写入count个字符,不包括'\0'字符,
并且不会在字符串末尾添加'\0'符,并且,如果字符串超过count,函数返回-1以标志可能导致的错误;gcc
中的snprintf函数的count参数表示,向buff中写入10个字符,包括'\0'字符,并且,返回实际的字符串长度,
例子中为12。  如果不了解这些函数在各平台间的差异,也许我们的程序在移植过程中会变得很脆弱。我们应该小心各种各样
的陷阱。

下面是MSDN对_snprintf函数的解释:

int _snprintf( char *buffersize_t countconst char *format [, argument] ... ); Parameters
buffer
Storage location for output
count
Maximum number of characters to store
format
Format-control string
argument
Optional arguments
Libraries

All versions of the C run-time libraries.

Return Value

_snprintf returns the number of bytes stored in buffer, not counting the terminating null character. If the number of bytes required to store the data exceeds count, then count bytes of data are stored in buffer and a negative value is returned.

Remarks

The _snprintf function formats and stores count or fewer characters and values (including a terminating null character that is always appended unless count is zero or the formatted string length is greater than or equal to count characters) 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. If copying occurs between strings that overlap, the behavior is undefined.



snprintf跟_snprintf的区别

snprintf函数并不是标准c/c++中规定的函数,但是在许多编译器中,厂商提供了其实现的版本。
在gcc中,该函数名称就snprintf,而在VC中称为_snprintf。
由于不是标准函数,没有一个统一的标准来规定该函数的行为,所以导致了各厂商间的实现版本可
能会有差异。今天也的的确确看到了差异,因为这个小小的差异是我的程序无法正常的处理数据。
这个小小的差异发生在count参数。在VC中,这个count就是要写入的总字符串字符数,例如:
//VC
int main(int argc, char * argv[])
...{
    char   buff[100];
     printf("%d ",_snprintf(buff,10,"1234567890ab"));
     printf("%s",buff);
    return 0;
}

//Linxu:gcc/g++
#include <stdio.h>
int main(int argc, char * argv[])
...{
    char   buff[100];
     printf("%d ",snprintf(buff,10,"1234567890ab"));
     printf("%s",buff);
    return 0;
}
vc程序的输出是:
-1
1234567890@
gcc程序的输出是:
12
123456789
从输出结果可以知道:VC中的_snprintf的count参数表示,会向buff中写入count个字符,不包括'\0'字符,
并且不会在字符串末尾添加'\0'符,并且,如果字符串超过count,函数返回-1以标志可能导致的错误;gcc
中的snprintf函数的count参数表示,向buff中写入10个字符,包括'\0'字符,并且,返回实际的字符串长度,
例子中为12。  如果不了解这些函数在各平台间的差异,也许我们的程序在移植过程中会变得很脆弱。我们应该小心各种各样
的陷阱。

下面是MSDN对_snprintf函数的解释:

int _snprintf( char *buffersize_t countconst char *format [, argument] ... ); Parameters
buffer
Storage location for output
count
Maximum number of characters to store
format
Format-control string
argument
Optional arguments
Libraries

All versions of the C run-time libraries.

Return Value

_snprintf returns the number of bytes stored in buffer, not counting the terminating null character. If the number of bytes required to store the data exceeds count, then count bytes of data are stored in buffer and a negative value is returned.

Remarks

The _snprintf function formats and stores count or fewer characters and values (including a terminating null character that is always appended unless count is zero or the formatted string length is greater than or equal to count characters) 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. If copying occurs between strings that overlap, the behavior is undefined.

snprintf跟_snprintf的区别

snprintf函数并不是标准c/c++中规定的函数,但是在许多编译器中,厂商提供了其实现的版本。
在gcc中,该函数名称就snprintf,而在VC中称为_snprintf。
由于不是标准函数,没有一个统一的标准来规定该函数的行为,所以导致了各厂商间的实现版本可
能会有差异。今天也的的确确看到了差异,因为这个小小的差异是我的程序无法正常的处理数据。
这个小小的差异发生在count参数。在VC中,这个count就是要写入的总字符串字符数,例如:
//VC
int main(int argc, char * argv[])
...{
    char   buff[100];
     printf("%d ",_snprintf(buff,10,"1234567890ab"));
     printf("%s",buff);
    return 0;
}

//Linxu:gcc/g++
#include <stdio.h>
int main(int argc, char * argv[])
...{
    char   buff[100];
     printf("%d ",snprintf(buff,10,"1234567890ab"));
     printf("%s",buff);
    return 0;
}
vc程序的输出是:
-1
1234567890@
gcc程序的输出是:
12
123456789
从输出结果可以知道:VC中的_snprintf的count参数表示,会向buff中写入count个字符,不包括'\0'字符,
并且不会在字符串末尾添加'\0'符,并且,如果字符串超过count,函数返回-1以标志可能导致的错误;gcc
中的snprintf函数的count参数表示,向buff中写入10个字符,包括'\0'字符,并且,返回实际的字符串长度,
例子中为12。  如果不了解这些函数在各平台间的差异,也许我们的程序在移植过程中会变得很脆弱。我们应该小心各种各样
的陷阱。

下面是MSDN对_snprintf函数的解释:

int _snprintf( char *buffersize_t countconst char *format [, argument] ... ); Parameters
buffer
Storage location for output
count
Maximum number of characters to store
format
Format-control string
argument
Optional arguments
Libraries

All versions of the C run-time libraries.

Return Value

_snprintf returns the number of bytes stored in buffer, not counting the terminating null character. If the number of bytes required to store the data exceeds count, then count bytes of data are stored in buffer and a negative value is returned.

Remarks

The _snprintf function formats and stores count or fewer characters and values (including a terminating null character that is always appended unless count is zero or the formatted string length is greater than or equal to count characters) 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. If copying occurs between strings that overlap, the behavior is undefined.


snprintf函数并不是标准c/c++中规定的函数,但是在许多编译器中,厂商提供了其实现的版本。
在gcc中,该函数名称就snprintf,而在VC中称为_snprintf。
由于不是标准函数,没有一个统一的标准来规定该函数的行为,所以导致了各厂商间的实现版本可
能会有差异。今天也的的确确看到了差异,因为这个小小的差异是我的程序无法正常的处理数据。
这个小小的差异发生在count参数。在VC中,这个count就是要写入的总字符串字符数,例如:
//VC

int main(int argc, char * argv[])
...{
    char   buff[100];
     printf("%d ",_snprintf(buff,10,"1234567890ab"));
     printf("%s",buff);
    return 0;
}


//Linxu:gcc/g++

#include <stdio.h>
int main(int argc, char * argv[])
...{
    char   buff[100];
     printf("%d ",snprintf(buff,10,"1234567890ab"));
     printf("%s",buff);
    return 0;
}
vc程序的输出是:
-1
1234567890@
gcc程序的输出是:
12
123456789
从输出结果可以知道:VC中的_snprintf的count参数表示,会向buff中写入count个字符,不包括'\0'字符,
并且不会在字符串末尾添加'\0'符,并且,如果字符串超过count,函数返回-1以标志可能导致的错误;gcc
中的snprintf函数的count参数表示,向buff中写入10个字符,包括'\0'字符,并且,返回实际的字符串长度,
例子中为12。  如果不了解这些函数在各平台间的差异,也许我们的程序在移植过程中会变得很脆弱。我们应该小心各种各样
的陷阱。

下面是MSDN对_snprintf函数的解释:

int _snprintf( char *buffersize_t countconst char *format [, argument] ... ); Parameters
buffer
Storage location for output
count
Maximum number of characters to store
format
Format-control string
argument
Optional arguments
Libraries

All versions of the C run-time libraries.

Return Value

_snprintf returns the number of bytes stored in buffer, not counting the terminating null character. If the number of bytes required to store the data exceeds count, then count bytes of data are stored in buffer and a negative value is returned.

Remarks

The _snprintf function formats and stores count or fewer characters and values (including a terminating null character that is always appended unless count is zero or the formatted string length is greater than or equal to count characters) 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. If copying occurs between strings that overlap, the behavior is undefined.



snprintf跟_snprintf的区别

snprintf函数并不是标准c/c++中规定的函数,但是在许多编译器中,厂商提供了其实现的版本。
在gcc中,该函数名称就snprintf,而在VC中称为_snprintf。
由于不是标准函数,没有一个统一的标准来规定该函数的行为,所以导致了各厂商间的实现版本可
能会有差异。今天也的的确确看到了差异,因为这个小小的差异是我的程序无法正常的处理数据。
这个小小的差异发生在count参数。在VC中,这个count就是要写入的总字符串字符数,例如:
//VC
int main(int argc, char * argv[])
...{
    char   buff[100];
     printf("%d ",_snprintf(buff,10,"1234567890ab"));
     printf("%s",buff);
    return 0;
}

//Linxu:gcc/g++
#include <stdio.h>
int main(int argc, char * argv[])
...{
    char   buff[100];
     printf("%d ",snprintf(buff,10,"1234567890ab"));
     printf("%s",buff);
    return 0;
}
vc程序的输出是:
-1
1234567890@
gcc程序的输出是:
12
123456789
从输出结果可以知道:VC中的_snprintf的count参数表示,会向buff中写入count个字符,不包括'\0'字符,
并且不会在字符串末尾添加'\0'符,并且,如果字符串超过count,函数返回-1以标志可能导致的错误;gcc
中的snprintf函数的count参数表示,向buff中写入10个字符,包括'\0'字符,并且,返回实际的字符串长度,
例子中为12。  如果不了解这些函数在各平台间的差异,也许我们的程序在移植过程中会变得很脆弱。我们应该小心各种各样
的陷阱。

下面是MSDN对_snprintf函数的解释:

int _snprintf( char *buffersize_t countconst char *format [, argument] ... ); Parameters
buffer
Storage location for output
count
Maximum number of characters to store
format
Format-control string
argument
Optional arguments
Libraries

All versions of the C run-time libraries.

Return Value

_snprintf returns the number of bytes stored in buffer, not counting the terminating null character. If the number of bytes required to store the data exceeds count, then count bytes of data are stored in buffer and a negative value is returned.

Remarks

The _snprintf function formats and stores count or fewer characters and values (including a terminating null character that is always appended unless count is zero or the formatted string length is greater than or equal to count characters) 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. If copying occurs between strings that overlap, the behavior is undefined.

snprintf跟_snprintf的区别

snprintf函数并不是标准c/c++中规定的函数,但是在许多编译器中,厂商提供了其实现的版本。
在gcc中,该函数名称就snprintf,而在VC中称为_snprintf。
由于不是标准函数,没有一个统一的标准来规定该函数的行为,所以导致了各厂商间的实现版本可
能会有差异。今天也的的确确看到了差异,因为这个小小的差异是我的程序无法正常的处理数据。
这个小小的差异发生在count参数。在VC中,这个count就是要写入的总字符串字符数,例如:
//VC
int main(int argc, char * argv[])
...{
    char   buff[100];
     printf("%d ",_snprintf(buff,10,"1234567890ab"));
     printf("%s",buff);
    return 0;
}

//Linxu:gcc/g++
#include <stdio.h>
int main(int argc, char * argv[])
...{
    char   buff[100];
     printf("%d ",snprintf(buff,10,"1234567890ab"));
     printf("%s",buff);
    return 0;
}
vc程序的输出是:
-1
1234567890@
gcc程序的输出是:
12
123456789
从输出结果可以知道:VC中的_snprintf的count参数表示,会向buff中写入count个字符,不包括'\0'字符,
并且不会在字符串末尾添加'\0'符,并且,如果字符串超过count,函数返回-1以标志可能导致的错误;gcc
中的snprintf函数的count参数表示,向buff中写入10个字符,包括'\0'字符,并且,返回实际的字符串长度,
例子中为12。  如果不了解这些函数在各平台间的差异,也许我们的程序在移植过程中会变得很脆弱。我们应该小心各种各样
的陷阱。

下面是MSDN对_snprintf函数的解释:

int _snprintf( char *buffersize_t countconst char *format [, argument] ... ); Parameters
buffer
Storage location for output
count
Maximum number of characters to store
format
Format-control string
argument
Optional arguments
Libraries

All versions of the C run-time libraries.

Return Value

_snprintf returns the number of bytes stored in buffer, not counting the terminating null character. If the number of bytes required to store the data exceeds count, then count bytes of data are stored in buffer and a negative value is returned.

Remarks

The _snprintf function formats and stores count or fewer characters and values (including a terminating null character that is always appended unless count is zero or the formatted string length is greater than or equal to count characters) 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. If copying occurs between strings that overlap, the behavior is undefined.


snprintf函数并不是标准c/c++中规定的函数,但是在许多编译器中,厂商提供了其实现的版本。
在gcc中,该函数名称就snprintf,而在VC中称为_snprintf。
由于不是标准函数,没有一个统一的标准来规定该函数的行为,所以导致了各厂商间的实现版本可
能会有差异。今天也的的确确看到了差异,因为这个小小的差异是我的程序无法正常的处理数据。
这个小小的差异发生在count参数。在VC中,这个count就是要写入的总字符串字符数,例如:
//VC

int main(int argc, char * argv[])
...{
    char   buff[100];
     printf("%d ",_snprintf(buff,10,"1234567890ab"));
     printf("%s",buff);
    return 0;
}


//Linxu:gcc/g++

#include <stdio.h>
int main(int argc, char * argv[])
...{
    char   buff[100];
     printf("%d ",snprintf(buff,10,"1234567890ab"));
     printf("%s",buff);
    return 0;
}
vc程序的输出是:
-1
1234567890@
gcc程序的输出是:
12
123456789
从输出结果可以知道:VC中的_snprintf的count参数表示,会向buff中写入count个字符,不包括'\0'字符,
并且不会在字符串末尾添加'\0'符,并且,如果字符串超过count,函数返回-1以标志可能导致的错误;gcc
中的snprintf函数的count参数表示,向buff中写入10个字符,包括'\0'字符,并且,返回实际的字符串长度,
例子中为12。  如果不了解这些函数在各平台间的差异,也许我们的程序在移植过程中会变得很脆弱。我们应该小心各种各样
的陷阱。

下面是MSDN对_snprintf函数的解释:

int _snprintf( char *buffersize_t countconst char *format [, argument] ... ); Parameters
buffer
Storage location for output
count
Maximum number of characters to store
format
Format-control string
argument
Optional arguments
Libraries

All versions of the C run-time libraries.

Return Value

_snprintf returns the number of bytes stored in buffer, not counting the terminating null character. If the number of bytes required to store the data exceeds count, then count bytes of data are stored in buffer and a negative value is returned.

Remarks

The _snprintf function formats and stores count or fewer characters and values (including a terminating null character that is always appended unless count is zero or the formatted string length is greater than or equal to count characters) 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. If copying occurs between strings that overlap, the behavior is undefined.



snprintf跟_snprintf的区别

snprintf函数并不是标准c/c++中规定的函数,但是在许多编译器中,厂商提供了其实现的版本。
在gcc中,该函数名称就snprintf,而在VC中称为_snprintf。
由于不是标准函数,没有一个统一的标准来规定该函数的行为,所以导致了各厂商间的实现版本可
能会有差异。今天也的的确确看到了差异,因为这个小小的差异是我的程序无法正常的处理数据。
这个小小的差异发生在count参数。在VC中,这个count就是要写入的总字符串字符数,例如:
//VC
int main(int argc, char * argv[])
...{
    char   buff[100];
     printf("%d ",_snprintf(buff,10,"1234567890ab"));
     printf("%s",buff);
    return 0;
}

//Linxu:gcc/g++
#include <stdio.h>
int main(int argc, char * argv[])
...{
    char   buff[100];
     printf("%d ",snprintf(buff,10,"1234567890ab"));
     printf("%s",buff);
    return 0;
}
vc程序的输出是:
-1
1234567890@
gcc程序的输出是:
12
123456789
从输出结果可以知道:VC中的_snprintf的count参数表示,会向buff中写入count个字符,不包括'\0'字符,
并且不会在字符串末尾添加'\0'符,并且,如果字符串超过count,函数返回-1以标志可能导致的错误;gcc
中的snprintf函数的count参数表示,向buff中写入10个字符,包括'\0'字符,并且,返回实际的字符串长度,
例子中为12。  如果不了解这些函数在各平台间的差异,也许我们的程序在移植过程中会变得很脆弱。我们应该小心各种各样
的陷阱。

下面是MSDN对_snprintf函数的解释:

int _snprintf( char *buffersize_t countconst char *format [, argument] ... ); Parameters
buffer
Storage location for output
count
Maximum number of characters to store
format
Format-control string
argument
Optional arguments
Libraries

All versions of the C run-time libraries.

Return Value

_snprintf returns the number of bytes stored in buffer, not counting the terminating null character. If the number of bytes required to store the data exceeds count, then count bytes of data are stored in buffer and a negative value is returned.

Remarks

The _snprintf function formats and stores count or fewer characters and values (including a terminating null character that is always appended unless count is zero or the formatted string length is greater than or equal to count characters) 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. If copying occurs between strings that overlap, the behavior is undefined.

snprintf跟_snprintf的区别

snprintf函数并不是标准c/c++中规定的函数,但是在许多编译器中,厂商提供了其实现的版本。
在gcc中,该函数名称就snprintf,而在VC中称为_snprintf。
由于不是标准函数,没有一个统一的标准来规定该函数的行为,所以导致了各厂商间的实现版本可
能会有差异。今天也的的确确看到了差异,因为这个小小的差异是我的程序无法正常的处理数据。
这个小小的差异发生在count参数。在VC中,这个count就是要写入的总字符串字符数,例如:
//VC
int main(int argc, char * argv[])
...{
    char   buff[100];
     printf("%d ",_snprintf(buff,10,"1234567890ab"));
     printf("%s",buff);
    return 0;
}

//Linxu:gcc/g++
#include <stdio.h>
int main(int argc, char * argv[])
...{
    char   buff[100];
     printf("%d ",snprintf(buff,10,"1234567890ab"));
     printf("%s",buff);
    return 0;
}
vc程序的输出是:
-1
1234567890@
gcc程序的输出是:
12
123456789
从输出结果可以知道:VC中的_snprintf的count参数表示,会向buff中写入count个字符,不包括'\0'字符,
并且不会在字符串末尾添加'\0'符,并且,如果字符串超过count,函数返回-1以标志可能导致的错误;gcc
中的snprintf函数的count参数表示,向buff中写入10个字符,包括'\0'字符,并且,返回实际的字符串长度,
例子中为12。  如果不了解这些函数在各平台间的差异,也许我们的程序在移植过程中会变得很脆弱。我们应该小心各种各样
的陷阱。

下面是MSDN对_snprintf函数的解释:

int _snprintf( char *buffersize_t countconst char *format [, argument] ... ); Parameters
buffer
Storage location for output
count
Maximum number of characters to store
format
Format-control string
argument
Optional arguments
Libraries

All versions of the C run-time libraries.

Return Value

_snprintf returns the number of bytes stored in buffer, not counting the terminating null character. If the number of bytes required to store the data exceeds count, then count bytes of data are stored in buffer and a negative value is returned.

Remarks

The _snprintf function formats and stores count or fewer characters and values (including a terminating null character that is always appended unless count is zero or the formatted string length is greater than or equal to count characters) 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. If copying occurs between strings that overlap, the behavior is undefined.


MSDN页面分别如下:

spirntf_s:

http://msdn.microsoft.com/zh-cn/library/ce3zzk1k%28VS.80%29.aspx

_snprintf:

http://msdn.microsoft.com/zh-cn/library/2ts7cx93%28v=VS.90%29.aspx

_snprintf_s:

http://msdn.microsoft.com/zh-cn/library/f30dzcf6.aspx

三个页面都有自己的例子。。其中后2个的例子比较多内容一些。

 

为免将来页面失效:

int sprintf_s(   char *buffer,   size_t sizeOfBuffer,   const char *format [,      argument] ... 

);

template <size_t size>int sprintf_s(   char (&buffer)[size],   const char *format [,      argument] ... 

); // C++ only

int _snprintf(   char *buffer,   size_t count,   const char *format [,      argument] ... 

);

int _snprintf(   char (&buffer)[size],   size_t count,   const char *format [,      argument] ... 

); // C++ only

int _snprintf_s(   char *buffer,   size_t sizeOfBuffer,   size_t count,   const char *format [,   argument] ... 

);

int _snprintf_s(   char (&buffer)[size],   size_t count,   const char *format [,   argument] ... 

); // C++ only

 

呼呼。。内容还挺多。

 

 

这里比较引人注目的是,_snprintf_s为什么在sizeOfBuffer的基础上,还要多加一个count?

count似乎是用来控制理想的宽度的。

如果得到的字符串超过了count,于是会被截断到count的长度后面再加一个null-teminate

当然,更高优先级的应该是sizeOfBuffer,必须不超过这个大小。这个就说到点子上了。

 

如果应该输出的字符串的大小已经达到了sizeOfBuffer,那么就溢出了。溢出的情况下,sprintf_s函数把这当做一个错误,会把buffer缓冲区置为一个空字符串""。

而_snprintf_s的好处就是,有了count参数,输出的字符串就算超过缓冲区长度,仍然会有输出,输出字符串被截断到count大小,在这个大小的字符串后面加null-teminate。

当然,如果count被设置成和sizeOfBuffer同样大,或者不合理的更大,那么这个count参数就失去了意义。

这时候,如果输出字符串将要达到或者超过sizeOfBuffer,一样导致一个错误,输出缓冲区被置为空字符串。

因此,如果希望缓冲区被尽量利用,可以把count参数置为_TRUNCATE,这样的情况下,实际上效果相当于是将count设置为sizeOfBuffer - 1。

 

 

至于C语言环境下,sprintf_s与_snprintf的对比:

注意到,_snprintf的参数用的是count,而sprintf_s的参数用的是sizeOfBuffer。这很能说明问题。

看下对_snprintf的说明:

Let len be the length of the formatted data string (not including the terminating null). len and count are in bytes for _snprintf, wide characters for _snwprintf.

If len < count, then len characters are stored in buffer, a null-terminator is appended, and len is returned.

If len = count, then len characters are stored in buffer, no null-terminator is appended, and len is returned.

If len > count, then count characters are stored in buffer, no null-terminator is appended, and a negative value is returned.

 

也就是说,_snprintf的count参数明明白白的就是一个count。

如果输出字符串刚好达到count,由于期待的最大长度就是count,那么输出字符串肯定要完整,不能截断。

但是假如字符串缓冲区的大小其实就是count,这怎么办?MS VCRT的设计者认为,在这种情况下应该把输出字符串的长度告知调用者,让调用者来决定是否自己添加null-teminate。

换句话说,调用_snprintf时要注意了,必须检查_snprintf的返回值,如果返回值不是正数,那么还得注意你的字符串缓冲区并不是null-teminate结尾的。

 

总结来说,sprintf_s在缓冲区不够大时会失败,失败时缓冲区中是一个空字符串。

_snprintf不会失败,但是必须注意如果缓冲区不够大,缓冲区的内容将不是null-teminate的,必须自己注意字符串的结束。

_snprintf_s结合了2者的优点,只要count参数设置合理,函数就不会因缓冲区不够而失败。

 

但是观察_snprintf_s的说明,有一个很有趣的内容。

这3族函数中,有失败情况的2个函数sprintf_s和_snprintf_s中,(再次强调,我这里的失败的意思是,调用后缓冲区里是一个空字符串),_set_invalid_parameter_handler设置的错误处理器,在失败的情况下会被调用。

 

而截断的情况下,错误处理器并不会被调用。

 

VC的库开发者总是提供一些怪怪的东西。无论如何,让代码更加安全总是符合大家的总体期望的。

另外补充一下,查阅这些字符串安全函数的资料的时候要注意,

对微软来说,凡是限制字符串复制长度的函数,这些设计者仍然认为是不安全的,因为逻辑上来说,

这些长度参数只是限制了源字符串被复制的长度,而不是目标缓冲区的长度。

也就是说,微软的这些设计者认为,安全的方式其实是依赖C++的机制,辨认出目标缓冲区的真正大小,以此实现安全的复制。




snprintf跟_snprintf的区别

snprintf函数并不是标准c/c++中规定的函数,但是在许多编译器中,厂商提供了其实现的版本。
在gcc中,该函数名称就snprintf,而在VC中称为_snprintf。
由于不是标准函数,没有一个统一的标准来规定该函数的行为,所以导致了各厂商间的实现版本可
能会有差异。今天也的的确确看到了差异,因为这个小小的差异是我的程序无法正常的处理数据。
这个小小的差异发生在count参数。在VC中,这个count就是要写入的总字符串字符数,例如:
//VC
int main(int argc, char * argv[])
...{
    char   buff[100];
     printf("%d ",_snprintf(buff,10,"1234567890ab"));
     printf("%s",buff);
    return 0;
}

//Linxu:gcc/g++
#include <stdio.h>
int main(int argc, char * argv[])
...{
    char   buff[100];
     printf("%d ",snprintf(buff,10,"1234567890ab"));
     printf("%s",buff);
    return 0;
}
vc程序的输出是:
-1
1234567890@
gcc程序的输出是:
12
123456789
从输出结果可以知道:VC中的_snprintf的count参数表示,会向buff中写入count个字符,不包括'\0'字符,
并且不会在字符串末尾添加'\0'符,并且,如果字符串超过count,函数返回-1以标志可能导致的错误;gcc
中的snprintf函数的count参数表示,向buff中写入10个字符,包括'\0'字符,并且,返回实际的字符串长度,
例子中为12。  如果不了解这些函数在各平台间的差异,也许我们的程序在移植过程中会变得很脆弱。我们应该小心各种各样
的陷阱。

下面是MSDN对_snprintf函数的解释:

int _snprintf( char *buffersize_t countconst char *format [, argument] ... ); Parameters
buffer
Storage location for output
count
Maximum number of characters to store
format
Format-control string
argument
Optional arguments
Libraries

All versions of the C run-time libraries.

Return Value

_snprintf returns the number of bytes stored in buffer, not counting the terminating null character. If the number of bytes required to store the data exceeds count, then count bytes of data are stored in buffer and a negative value is returned.

Remarks

The _snprintf function formats and stores count or fewer characters and values (including a terminating null character that is always appended unless count is zero or the formatted string length is greater than or equal to count characters) 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. If copying occurs between strings that overlap, the behavior is undefined.

snprintf跟_snprintf的区别

snprintf函数并不是标准c/c++中规定的函数,但是在许多编译器中,厂商提供了其实现的版本。
在gcc中,该函数名称就snprintf,而在VC中称为_snprintf。
由于不是标准函数,没有一个统一的标准来规定该函数的行为,所以导致了各厂商间的实现版本可
能会有差异。今天也的的确确看到了差异,因为这个小小的差异是我的程序无法正常的处理数据。
这个小小的差异发生在count参数。在VC中,这个count就是要写入的总字符串字符数,例如:
//VC
int main(int argc, char * argv[])
...{
    char   buff[100];
     printf("%d ",_snprintf(buff,10,"1234567890ab"));
     printf("%s",buff);
    return 0;
}

//Linxu:gcc/g++
#include <stdio.h>
int main(int argc, char * argv[])
...{
    char   buff[100];
     printf("%d ",snprintf(buff,10,"1234567890ab"));
     printf("%s",buff);
    return 0;
}
vc程序的输出是:
-1
1234567890@
gcc程序的输出是:
12
123456789
从输出结果可以知道:VC中的_snprintf的count参数表示,会向buff中写入count个字符,不包括'\0'字符,
并且不会在字符串末尾添加'\0'符,并且,如果字符串超过count,函数返回-1以标志可能导致的错误;gcc
中的snprintf函数的count参数表示,向buff中写入10个字符,包括'\0'字符,并且,返回实际的字符串长度,
例子中为12。  如果不了解这些函数在各平台间的差异,也许我们的程序在移植过程中会变得很脆弱。我们应该小心各种各样
的陷阱。

下面是MSDN对_snprintf函数的解释:

int _snprintf( char *buffersize_t countconst char *format [, argument] ... ); Parameters
buffer
Storage location for output
count
Maximum number of characters to store
format
Format-control string
argument
Optional arguments
Libraries

All versions of the C run-time libraries.

Return Value

_snprintf returns the number of bytes stored in buffer, not counting the terminating null character. If the number of bytes required to store the data exceeds count, then count bytes of data are stored in buffer and a negative value is returned.

Remarks

The _snprintf function formats and stores count or fewer characters and values (including a terminating null character that is always appended unless count is zero or the formatted string length is greater than or equal to count characters) 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. If copying occurs between strings that overlap, the behavior is undefined.


snprintf函数并不是标准c/c++中规定的函数,但是在许多编译器中,厂商提供了其实现的版本。
在gcc中,该函数名称就snprintf,而在VC中称为_snprintf。
由于不是标准函数,没有一个统一的标准来规定该函数的行为,所以导致了各厂商间的实现版本可
能会有差异。今天也的的确确看到了差异,因为这个小小的差异是我的程序无法正常的处理数据。
这个小小的差异发生在count参数。在VC中,这个count就是要写入的总字符串字符数,例如:
//VC

int main(int argc, char * argv[])
...{
    char   buff[100];
     printf("%d ",_snprintf(buff,10,"1234567890ab"));
     printf("%s",buff);
    return 0;
}


//Linxu:gcc/g++

#include <stdio.h>
int main(int argc, char * argv[])
...{
    char   buff[100];
     printf("%d ",snprintf(buff,10,"1234567890ab"));
     printf("%s",buff);
    return 0;
}
vc程序的输出是:
-1
1234567890@
gcc程序的输出是:
12
123456789
从输出结果可以知道:VC中的_snprintf的count参数表示,会向buff中写入count个字符,不包括'\0'字符,
并且不会在字符串末尾添加'\0'符,并且,如果字符串超过count,函数返回-1以标志可能导致的错误;gcc
中的snprintf函数的count参数表示,向buff中写入10个字符,包括'\0'字符,并且,返回实际的字符串长度,
例子中为12。  如果不了解这些函数在各平台间的差异,也许我们的程序在移植过程中会变得很脆弱。我们应该小心各种各样
的陷阱。

下面是MSDN对_snprintf函数的解释:

int _snprintf( char *buffersize_t countconst char *format [, argument] ... ); Parameters
buffer
Storage location for output
count
Maximum number of characters to store
format
Format-control string
argument
Optional arguments
Libraries

All versions of the C run-time libraries.

Return Value

_snprintf returns the number of bytes stored in buffer, not counting the terminating null character. If the number of bytes required to store the data exceeds count, then count bytes of data are stored in buffer and a negative value is returned.

Remarks

The _snprintf function formats and stores count or fewer characters and values (including a terminating null character that is always appended unless count is zero or the formatted string length is greater than or equal to count characters) 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. If copying occurs between strings that overlap, the behavior is undefined.



snprintf跟_snprintf的区别

snprintf函数并不是标准c/c++中规定的函数,但是在许多编译器中,厂商提供了其实现的版本。
在gcc中,该函数名称就snprintf,而在VC中称为_snprintf。
由于不是标准函数,没有一个统一的标准来规定该函数的行为,所以导致了各厂商间的实现版本可
能会有差异。今天也的的确确看到了差异,因为这个小小的差异是我的程序无法正常的处理数据。
这个小小的差异发生在count参数。在VC中,这个count就是要写入的总字符串字符数,例如:
//VC
int main(int argc, char * argv[])
...{
    char   buff[100];
     printf("%d ",_snprintf(buff,10,"1234567890ab"));
     printf("%s",buff);
    return 0;
}

//Linxu:gcc/g++
#include <stdio.h>
int main(int argc, char * argv[])
...{
    char   buff[100];
     printf("%d ",snprintf(buff,10,"1234567890ab"));
     printf("%s",buff);
    return 0;
}
vc程序的输出是:
-1
1234567890@
gcc程序的输出是:
12
123456789
从输出结果可以知道:VC中的_snprintf的count参数表示,会向buff中写入count个字符,不包括'\0'字符,
并且不会在字符串末尾添加'\0'符,并且,如果字符串超过count,函数返回-1以标志可能导致的错误;gcc
中的snprintf函数的count参数表示,向buff中写入10个字符,包括'\0'字符,并且,返回实际的字符串长度,
例子中为12。  如果不了解这些函数在各平台间的差异,也许我们的程序在移植过程中会变得很脆弱。我们应该小心各种各样
的陷阱。

下面是MSDN对_snprintf函数的解释:

int _snprintf( char *buffersize_t countconst char *format [, argument] ... ); Parameters
buffer
Storage location for output
count
Maximum number of characters to store
format
Format-control string
argument
Optional arguments
Libraries

All versions of the C run-time libraries.

Return Value

_snprintf returns the number of bytes stored in buffer, not counting the terminating null character. If the number of bytes required to store the data exceeds count, then count bytes of data are stored in buffer and a negative value is returned.

Remarks

The _snprintf function formats and stores count or fewer characters and values (including a terminating null character that is always appended unless count is zero or the formatted string length is greater than or equal to count characters) 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. If copying occurs between strings that overlap, the behavior is undefined.

snprintf跟_snprintf的区别

snprintf函数并不是标准c/c++中规定的函数,但是在许多编译器中,厂商提供了其实现的版本。
在gcc中,该函数名称就snprintf,而在VC中称为_snprintf。
由于不是标准函数,没有一个统一的标准来规定该函数的行为,所以导致了各厂商间的实现版本可
能会有差异。今天也的的确确看到了差异,因为这个小小的差异是我的程序无法正常的处理数据。
这个小小的差异发生在count参数。在VC中,这个count就是要写入的总字符串字符数,例如:
//VC
int main(int argc, char * argv[])
...{
    char   buff[100];
     printf("%d ",_snprintf(buff,10,"1234567890ab"));
     printf("%s",buff);
    return 0;
}

//Linxu:gcc/g++
#include <stdio.h>
int main(int argc, char * argv[])
...{
    char   buff[100];
     printf("%d ",snprintf(buff,10,"1234567890ab"));
     printf("%s",buff);
    return 0;
}
vc程序的输出是:
-1
1234567890@
gcc程序的输出是:
12
123456789
从输出结果可以知道:VC中的_snprintf的count参数表示,会向buff中写入count个字符,不包括'\0'字符,
并且不会在字符串末尾添加'\0'符,并且,如果字符串超过count,函数返回-1以标志可能导致的错误;gcc
中的snprintf函数的count参数表示,向buff中写入10个字符,包括'\0'字符,并且,返回实际的字符串长度,
例子中为12。  如果不了解这些函数在各平台间的差异,也许我们的程序在移植过程中会变得很脆弱。我们应该小心各种各样
的陷阱。

下面是MSDN对_snprintf函数的解释:

int _snprintf( char *buffersize_t countconst char *format [, argument] ... ); Parameters
buffer
Storage location for output
count
Maximum number of characters to store
format
Format-control string
argument
Optional arguments
Libraries

All versions of the C run-time libraries.

Return Value

_snprintf returns the number of bytes stored in buffer, not counting the terminating null character. If the number of bytes required to store the data exceeds count, then count bytes of data are stored in buffer and a negative value is returned.

Remarks

The _snprintf function formats and stores count or fewer characters and values (including a terminating null character that is always appended unless count is zero or the formatted string length is greater than or equal to count characters) 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. If copying occurs between strings that overlap, the behavior is undefined.


snprintf函数并不是标准c/c++中规定的函数,但是在许多编译器中,厂商提供了其实现的版本。
在gcc中,该函数名称就snprintf,而在VC中称为_snprintf。
由于不是标准函数,没有一个统一的标准来规定该函数的行为,所以导致了各厂商间的实现版本可
能会有差异。今天也的的确确看到了差异,因为这个小小的差异是我的程序无法正常的处理数据。
这个小小的差异发生在count参数。在VC中,这个count就是要写入的总字符串字符数,例如:
//VC

int main(int argc, char * argv[])
...{
    char   buff[100];
     printf("%d ",_snprintf(buff,10,"1234567890ab"));
     printf("%s",buff);
    return 0;
}


//Linxu:gcc/g++

#include <stdio.h>
int main(int argc, char * argv[])
...{
    char   buff[100];
     printf("%d ",snprintf(buff,10,"1234567890ab"));
     printf("%s",buff);
    return 0;
}
vc程序的输出是:
-1
1234567890@
gcc程序的输出是:
12
123456789
从输出结果可以知道:VC中的_snprintf的count参数表示,会向buff中写入count个字符,不包括'\0'字符,
并且不会在字符串末尾添加'\0'符,并且,如果字符串超过count,函数返回-1以标志可能导致的错误;gcc
中的snprintf函数的count参数表示,向buff中写入10个字符,包括'\0'字符,并且,返回实际的字符串长度,
例子中为12。  如果不了解这些函数在各平台间的差异,也许我们的程序在移植过程中会变得很脆弱。我们应该小心各种各样
的陷阱。

下面是MSDN对_snprintf函数的解释:

int _snprintf( char *buffersize_t countconst char *format [, argument] ... ); Parameters
buffer
Storage location for output
count
Maximum number of characters to store
format
Format-control string
argument
Optional arguments
Libraries

All versions of the C run-time libraries.

Return Value

_snprintf returns the number of bytes stored in buffer, not counting the terminating null character. If the number of bytes required to store the data exceeds count, then count bytes of data are stored in buffer and a negative value is returned.

Remarks

The _snprintf function formats and stores count or fewer characters and values (including a terminating null character that is always appended unless count is zero or the formatted string length is greater than or equal to count characters) 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. If copying occurs between strings that overlap, the behavior is undefined.



snprintf跟_snprintf的区别

snprintf函数并不是标准c/c++中规定的函数,但是在许多编译器中,厂商提供了其实现的版本。
在gcc中,该函数名称就snprintf,而在VC中称为_snprintf。
由于不是标准函数,没有一个统一的标准来规定该函数的行为,所以导致了各厂商间的实现版本可
能会有差异。今天也的的确确看到了差异,因为这个小小的差异是我的程序无法正常的处理数据。
这个小小的差异发生在count参数。在VC中,这个count就是要写入的总字符串字符数,例如:
//VC
int main(int argc, char * argv[])
...{
    char   buff[100];
     printf("%d ",_snprintf(buff,10,"1234567890ab"));
     printf("%s",buff);
    return 0;
}

//Linxu:gcc/g++
#include <stdio.h>
int main(int argc, char * argv[])
...{
    char   buff[100];
     printf("%d ",snprintf(buff,10,"1234567890ab"));
     printf("%s",buff);
    return 0;
}
vc程序的输出是:
-1
1234567890@
gcc程序的输出是:
12
123456789
从输出结果可以知道:VC中的_snprintf的count参数表示,会向buff中写入count个字符,不包括'\0'字符,
并且不会在字符串末尾添加'\0'符,并且,如果字符串超过count,函数返回-1以标志可能导致的错误;gcc
中的snprintf函数的count参数表示,向buff中写入10个字符,包括'\0'字符,并且,返回实际的字符串长度,
例子中为12。  如果不了解这些函数在各平台间的差异,也许我们的程序在移植过程中会变得很脆弱。我们应该小心各种各样
的陷阱。

下面是MSDN对_snprintf函数的解释:

int _snprintf( char *buffersize_t countconst char *format [, argument] ... ); Parameters
buffer
Storage location for output
count
Maximum number of characters to store
format
Format-control string
argument
Optional arguments
Libraries

All versions of the C run-time libraries.

Return Value

_snprintf returns the number of bytes stored in buffer, not counting the terminating null character. If the number of bytes required to store the data exceeds count, then count bytes of data are stored in buffer and a negative value is returned.

Remarks

The _snprintf function formats and stores count or fewer characters and values (including a terminating null character that is always appended unless count is zero or the formatted string length is greater than or equal to count characters) 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. If copying occurs between strings that overlap, the behavior is undefined.

snprintf跟_snprintf的区别

snprintf函数并不是标准c/c++中规定的函数,但是在许多编译器中,厂商提供了其实现的版本。
在gcc中,该函数名称就snprintf,而在VC中称为_snprintf。
由于不是标准函数,没有一个统一的标准来规定该函数的行为,所以导致了各厂商间的实现版本可
能会有差异。今天也的的确确看到了差异,因为这个小小的差异是我的程序无法正常的处理数据。
这个小小的差异发生在count参数。在VC中,这个count就是要写入的总字符串字符数,例如:
//VC
int main(int argc, char * argv[])
...{
    char   buff[100];
     printf("%d ",_snprintf(buff,10,"1234567890ab"));
     printf("%s",buff);
    return 0;
}

//Linxu:gcc/g++
#include <stdio.h>
int main(int argc, char * argv[])
...{
    char   buff[100];
     printf("%d ",snprintf(buff,10,"1234567890ab"));
     printf("%s",buff);
    return 0;
}
vc程序的输出是:
-1
1234567890@
gcc程序的输出是:
12
123456789
从输出结果可以知道:VC中的_snprintf的count参数表示,会向buff中写入count个字符,不包括'\0'字符,
并且不会在字符串末尾添加'\0'符,并且,如果字符串超过count,函数返回-1以标志可能导致的错误;gcc
中的snprintf函数的count参数表示,向buff中写入10个字符,包括'\0'字符,并且,返回实际的字符串长度,
例子中为12。  如果不了解这些函数在各平台间的差异,也许我们的程序在移植过程中会变得很脆弱。我们应该小心各种各样
的陷阱。

下面是MSDN对_snprintf函数的解释:

int _snprintf( char *buffersize_t countconst char *format [, argument] ... ); Parameters
buffer
Storage location for output
count
Maximum number of characters to store
format
Format-control string
argument
Optional arguments
Libraries

All versions of the C run-time libraries.

Return Value

_snprintf returns the number of bytes stored in buffer, not counting the terminating null character. If the number of bytes required to store the data exceeds count, then count bytes of data are stored in buffer and a negative value is returned.

Remarks

The _snprintf function formats and stores count or fewer characters and values (including a terminating null character that is always appended unless count is zero or the formatted string length is greater than or equal to count characters) 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. If copying occurs between strings that overlap, the behavior is undefined.


snprintf函数并不是标准c/c++中规定的函数,但是在许多编译器中,厂商提供了其实现的版本。
在gcc中,该函数名称就snprintf,而在VC中称为_snprintf。
由于不是标准函数,没有一个统一的标准来规定该函数的行为,所以导致了各厂商间的实现版本可
能会有差异。今天也的的确确看到了差异,因为这个小小的差异是我的程序无法正常的处理数据。
这个小小的差异发生在count参数。在VC中,这个count就是要写入的总字符串字符数,例如:
//VC

int main(int argc, char * argv[])
...{
    char   buff[100];
     printf("%d ",_snprintf(buff,10,"1234567890ab"));
     printf("%s",buff);
    return 0;
}


//Linxu:gcc/g++

#include <stdio.h>
int main(int argc, char * argv[])
...{
    char   buff[100];
     printf("%d ",snprintf(buff,10,"1234567890ab"));
     printf("%s",buff);
    return 0;
}
vc程序的输出是:
-1
1234567890@
gcc程序的输出是:
12
123456789
从输出结果可以知道:VC中的_snprintf的count参数表示,会向buff中写入count个字符,不包括'\0'字符,
并且不会在字符串末尾添加'\0'符,并且,如果字符串超过count,函数返回-1以标志可能导致的错误;gcc
中的snprintf函数的count参数表示,向buff中写入10个字符,包括'\0'字符,并且,返回实际的字符串长度,
例子中为12。  如果不了解这些函数在各平台间的差异,也许我们的程序在移植过程中会变得很脆弱。我们应该小心各种各样
的陷阱。

下面是MSDN对_snprintf函数的解释:

int _snprintf( char *buffersize_t countconst char *format [, argument] ... ); Parameters
buffer
Storage location for output
count
Maximum number of characters to store
format
Format-control string
argument
Optional arguments
Libraries

All versions of the C run-time libraries.

Return Value

_snprintf returns the number of bytes stored in buffer, not counting the terminating null character. If the number of bytes required to store the data exceeds count, then count bytes of data are stored in buffer and a negative value is returned.

Remarks

The _snprintf function formats and stores count or fewer characters and values (including a terminating null character that is always appended unless count is zero or the formatted string length is greater than or equal to count characters) 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. If copying occurs between strings that overlap, the behavior is undefined.



snprintf跟_snprintf的区别

snprintf函数并不是标准c/c++中规定的函数,但是在许多编译器中,厂商提供了其实现的版本。
在gcc中,该函数名称就snprintf,而在VC中称为_snprintf。
由于不是标准函数,没有一个统一的标准来规定该函数的行为,所以导致了各厂商间的实现版本可
能会有差异。今天也的的确确看到了差异,因为这个小小的差异是我的程序无法正常的处理数据。
这个小小的差异发生在count参数。在VC中,这个count就是要写入的总字符串字符数,例如:
//VC
int main(int argc, char * argv[])
...{
    char   buff[100];
     printf("%d ",_snprintf(buff,10,"1234567890ab"));
     printf("%s",buff);
    return 0;
}

//Linxu:gcc/g++
#include <stdio.h>
int main(int argc, char * argv[])
...{
    char   buff[100];
     printf("%d ",snprintf(buff,10,"1234567890ab"));
     printf("%s",buff);
    return 0;
}
vc程序的输出是:
-1
1234567890@
gcc程序的输出是:
12
123456789
从输出结果可以知道:VC中的_snprintf的count参数表示,会向buff中写入count个字符,不包括'\0'字符,
并且不会在字符串末尾添加'\0'符,并且,如果字符串超过count,函数返回-1以标志可能导致的错误;gcc
中的snprintf函数的count参数表示,向buff中写入10个字符,包括'\0'字符,并且,返回实际的字符串长度,
例子中为12。  如果不了解这些函数在各平台间的差异,也许我们的程序在移植过程中会变得很脆弱。我们应该小心各种各样
的陷阱。

下面是MSDN对_snprintf函数的解释:

int _snprintf( char *buffersize_t countconst char *format [, argument] ... ); Parameters
buffer
Storage location for output
count
Maximum number of characters to store
format
Format-control string
argument
Optional arguments
Libraries

All versions of the C run-time libraries.

Return Value

_snprintf returns the number of bytes stored in buffer, not counting the terminating null character. If the number of bytes required to store the data exceeds count, then count bytes of data are stored in buffer and a negative value is returned.

Remarks

The _snprintf function formats and stores count or fewer characters and values (including a terminating null character that is always appended unless count is zero or the formatted string length is greater than or equal to count characters) 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. If copying occurs between strings that overlap, the behavior is undefined.

snprintf跟_snprintf的区别

snprintf函数并不是标准c/c++中规定的函数,但是在许多编译器中,厂商提供了其实现的版本。
在gcc中,该函数名称就snprintf,而在VC中称为_snprintf。
由于不是标准函数,没有一个统一的标准来规定该函数的行为,所以导致了各厂商间的实现版本可
能会有差异。今天也的的确确看到了差异,因为这个小小的差异是我的程序无法正常的处理数据。
这个小小的差异发生在count参数。在VC中,这个count就是要写入的总字符串字符数,例如:
//VC
int main(int argc, char * argv[])
...{
    char   buff[100];
     printf("%d ",_snprintf(buff,10,"1234567890ab"));
     printf("%s",buff);
    return 0;
}

//Linxu:gcc/g++
#include <stdio.h>
int main(int argc, char * argv[])
...{
    char   buff[100];
     printf("%d ",snprintf(buff,10,"1234567890ab"));
     printf("%s",buff);
    return 0;
}
vc程序的输出是:
-1
1234567890@
gcc程序的输出是:
12
123456789
从输出结果可以知道:VC中的_snprintf的count参数表示,会向buff中写入count个字符,不包括'\0'字符,
并且不会在字符串末尾添加'\0'符,并且,如果字符串超过count,函数返回-1以标志可能导致的错误;gcc
中的snprintf函数的count参数表示,向buff中写入10个字符,包括'\0'字符,并且,返回实际的字符串长度,
例子中为12。  如果不了解这些函数在各平台间的差异,也许我们的程序在移植过程中会变得很脆弱。我们应该小心各种各样
的陷阱。

下面是MSDN对_snprintf函数的解释:

int _snprintf( char *buffersize_t countconst char *format [, argument] ... ); Parameters
buffer
Storage location for output
count
Maximum number of characters to store
format
Format-control string
argument
Optional arguments
Libraries

All versions of the C run-time libraries.

Return Value

_snprintf returns the number of bytes stored in buffer, not counting the terminating null character. If the number of bytes required to store the data exceeds count, then count bytes of data are stored in buffer and a negative value is returned.

Remarks

The _snprintf function formats and stores count or fewer characters and values (including a terminating null character that is always appended unless count is zero or the formatted string length is greater than or equal to count characters) 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. If copying occurs between strings that overlap, the behavior is undefined.

原创粉丝点击