请讲一下wsprintf()与strncpy()的区别?

tianyxy 2004-01-05 12:18:54
如题!
...全文
83 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
jeremy_dym 2004-07-26
  • 打赏
  • 举报
回复
你好,现有一紧急求助:
对于一下一个非常简单的test_sprint.c源代码:
#include <string.h>
#include <stdio.h>
void main(void)
{unsigned char str[20];
int i=10000;
memset(str,0,sizeof(str));
sprintf(str,"\nLinux %d岁!!! \n",i);
printf("%s",str);

}
对于这中中西文混合的printf,我在turboc2.0下编译和运行没有问题;但是在Linux下GCC编译没有问题,但是运行出错,提示"Invalid multibyte format string ",请问这是什么原因,与Linux中文本地化有关系吗?
我的联系方式:dengyueming@tom.com,非常感谢!
柯本 2004-01-09
  • 打赏
  • 举报
回复
char *strncpy(char *dest, const char *src, size_t maxlen);
将字符从src拷贝到dest,最长为maxlen(如果src的长度大于maxlen)
它是标准的c/c++函数
int wsprintf(LPTSTR lpOut,LPCTSTR lpFmt, ... );
是格式化输出到一字串,它是由microsoft windows 提供的,不是标准的c/c++函数
API为application programming interfac应用程序接口,是由操作系统提供给开发工具的接口
函数,它只能在特定的操作系统下使用
你可用标准的sprintf代替wsprintf

tianyxy 2004-01-07
  • 打赏
  • 举报
回复
能不能解释一下win API?
C/C++函数,两个都可以用呀!!
请keiy()指教!
柯本 2004-01-05
  • 打赏
  • 举报
回复
有没有写错?
wsprintf是win API
strncpy是标准C/C++函数
::
The wsprintf function formats and stores a series of characters and values in a buffer. Any arguments are converted and copied to the output buffer according to the corresponding format specification in the format string. The function appends a terminating null character to the characters it writes, but the return value does not include the terminating null character in its character count.

int wsprintf(

LPTSTR lpOut, // pointer to buffer for output
LPCTSTR lpFmt, // pointer to format-control string
... // optional arguments
);


Parameters

lpOut

Points to a buffer to receive the formatted output.

lpFmt

Points to a null-terminated string that contains the format-control specifications. In addition to ordinary ASCII characters, a format specification for each argument appears in this string. For more information about the format specification, see the Remarks section.

...

Specifies one or more optional arguments. The number and type of argument parameters depend on the corresponding format-control specifications in the lpFmt parameter.

------------------------------
strncpy Category

Memory and String Manipulation Routines, Inline Routines

Prototype

char *strncpy(char *dest, const char *src, size_t maxlen);

wchar_t *wcsncpy(wchar_t *dest, const wchar_t *src, size_t maxlen);

unsigned char *_mbsncpy(unsigned char *dest, const unsigned char *src, size_t maxlen);

Description

Copies a given number of bytes from one string into another, truncating or padding as necessary.

strncpy copies up to maxlen characters from src into dest, truncating or null-padding dest. The target string, dest, might not be null-terminated if the length of src is maxlen or more.

Return Value

strncpy returns dest.
tianyxy 2004-01-05
  • 打赏
  • 举报
回复
这个函数wsprintf() 我在用呀 怎么会没有呢?
----按格式拷贝字符串呀 如:
wsprintf(char * string1,"%s,one string,%s....",string1,string2.....
justhim 2004-01-05
  • 打赏
  • 举报
回复
没有wsprintf这个函数们应该是swprintf, swprintf是sprintf的宽字符版本。
int sprintf(char *str, const char *format, ...);
int swprintf(wchar_t *wcs, size_t maxlen, const wchar_t *format, ...);
maxlen为写到wcs的最大字符数。
printf系列更灵活,不过速度也比较慢。
char *strncpy(char *dest, const char *src, size_t n);
strncpy把src中最多n个字符拷到dest中,src必须是NULL结尾的字符串指针,遇到NULL
停止拷贝。


69,371

社区成员

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

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