sprintf错误在视觉工作室 [英] sprintf error in visual studio

查看:420
本文介绍了sprintf错误在视觉工作室的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Windows visual studio中使用UDP套接字程式。
我得到sprintf语句的错误。如何纠正?
我的代码是:

  for(i = 0; i <30; i ++) 
{
printf(send AT * REF:take off\);
sprintf(command,AT * REF =%d,290718208\r,seq ++);



rc = sendto(sd,command,strlen(command)+ 1,flags,(struct sockaddr *)& droneAddr,sizeof(droneAddr));
if(rc< 0){
printf(%s:can not send data\\\
,argv [0]);
return(1);
}
}

我得到的错误是:

 错误1错误C4996:'sprintf':此函数或变量可能不安全。请考虑使用sprintf_s。要禁用弃用,请使用_CRT_SECURE_NO_WARNINGS。 
错误1错误LNK2019:未解析的外部符号__imp__bind @ 12
错误2错误LNK2019:未解析的外部符号__imp__htonl @ 4在函数_main中引用
错误3错误LNK2019:未解决的外部符号__imp__htons @ 4
错误4错误LNK2019:未解析的外部符号__imp__sendto @ 24
错误5错误LNK2019:未解析的外部符号__imp__socket @ 12
错误6错误LNK2019:未解决的外部符号__imp__gethostbyname @ 4
错误7错误LNK1120:6未解析的外部


解决方案

printf ,并且其姐妹函数 sprintf 被视为不安全,因为如果使用不正确,它们会发出未定义的行为。



Visual Studio默认禁用这些函数。



但是,因为它们是C ++标准库的一部分,您可以 使用它们。但是Visual Studio只允许你这样做,如果你包括

  #define _CRT_SECURE_NO_WARNINGS 


或者,包括<$>



(顺便说一句,Visual Studio正在发出一个指令,它会在你的项目预处理程序设置中发出一个指令,并且会发出一个错误信息,例如:c $ c> _CRT_SECURE_NO_WARNINGS 非常有帮助的错误消息在这种情况下,尝试学会解释他们。)


I am working on a UDP socket program in windows visual studio. I am getting an error for sprintf statement. How to correct it? My code is:

    for (i = 0; i < 30;i++) //take-off
{
    printf("send AT*REF:take off\n");
            sprintf(command, "AT*REF=%d,290718208\r", seq++);



    rc = sendto(sd, command, strlen(command) + 1, flags, (struct sockaddr *) &droneAddr, sizeof(droneAddr));
    if (rc<0) {
        printf("%s: can not send data\n", argv[0]);
            return(1);
                    }
     }

The errors I am getting are :

Error   1 error C4996: 'sprintf': This function or variable may be unsafe. Consider using sprintf_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS.
Error   1 error LNK2019: unresolved external symbol __imp__bind@12
Error   2 error LNK2019: unresolved external symbol __imp__htonl@4 referenced in function _main 
Error   3 error LNK2019: unresolved external symbol __imp__htons@4
Error   4 error LNK2019: unresolved external symbol __imp__sendto@24
Error   5 error LNK2019: unresolved external symbol __imp__socket@12
Error   6 error LNK2019: unresolved external symbol __imp__gethostbyname@4
Error   7 error LNK1120: 6 unresolved externals 

解决方案

printf and its sister function sprintf are considered unsafe due to the amount of undefined behaviour they emit if used incorrectly.

Visual Studio is disabling these functions by default.

But, since they are part of the C++ standard library, you can use them. But Visual Studio will only allow you to do that if you include the line

#define _CRT_SECURE_NO_WARNINGS

before the relevant standard library headers are included.

Alternatively, include _CRT_SECURE_NO_WARNINGS in your project preprocessor settings (which is what I do).

(By the way, Visual Studio is emitting a very helpful error message in this instance. Do try to learn to interpret them.)

这篇关于sprintf错误在视觉工作室的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆