_vsctprintf函数是不是跨平台的?

clever101
博客专家认证
2019-08-20 06:50:15
以前有人在论坛里问了一个问题:

如何确定vsprintf目的字符串缓冲区该预先申请多大?

今天发现微软提供了叫_vscprintf的函数可以来帮你计算缓冲区的长度的,具体用法如下:

// crt_vsprintf.c
// compile with: /W3
// This program uses vsprintf to write to a buffer.
// The size of the buffer is determined by _vscprintf.

#include <stdlib.h>
#include <stdio.h>
#include <stdarg.h>

void test( char * format, ... )
{
va_list args;
int len;
char *buffer;

// retrieve the variable arguments
va_start( args, format );

len = _vscprintf( format, args ) // _vscprintf doesn't count
+ 1; // terminating '\0'

buffer = (char*)malloc( len * sizeof(char) );

vsprintf( buffer, format, args ); // C4996
// Note: vsprintf is deprecated; consider using vsprintf_s instead
puts( buffer );

free( buffer );
}

int main( void )
{
test( "%d %c %d", 123, '<', 456 );
test( "%s", "This is a string" );
}


不过我不知_vsctprintf函数能否在Linux平台下使用。
...全文
464 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
clever101 2019-12-04
  • 打赏
  • 举报
回复
赵4老师 2019-08-21
  • 打赏
  • 举报
回复
Linux下要学会使用man命令比如 man vsprintf man 1 vsprintf man 2 vsprintf man 3 vsprintf man 3p vsprintf
  • 打赏
  • 举报
回复
这个函数需要:Required for UNIX V compatibility. 需要兼容UNIX V 如果不兼容,可以用fprintf,输出到文件,返回值就是大小了。 然后再分配。
铖邑 2019-08-20
  • 打赏
  • 举报
回复
linux底下也是可以的,这个代码十多年前写过,现在一时半会儿不知道在哪里,哪天找到了告诉你

69,382

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧