snprintf函数截断问题

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>

int main()
{
    //printf("Hello World");
    char buff[12] = {0};
    snprintf(buff,sizeof(buff),"%s\n","hello world");
    printf("the %s an the \n",buff);
    return 0;
}

在编译阶段会报出字符阶段的错误,

main.c:18:37: warning: ‘snprintf’ output truncated before the last format character [-Wformat-truncation=]
   18 |     snprintf(buff,sizeof(buff),"%s\n","hello world");
      |                                     ^
main.c:18:5: note: ‘snprintf’ output 13 bytes into a destination of size 12
   18 |     snprintf(buff,sizeof(buff),"%s\n","hello world");
      |     ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
the hello world an the 

运行可以看到最后的\n并没有写入到buff中导致字符发生了截断

修改只需要在字符串的长度上加上尾零的长度和\n字符的长度

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>

int main()
{
    //printf("Hello World");
    char buff[13] = {0};
    snprintf(buff,sizeof(buff),"%s\n","hello world");
    printf("the %s an the sizeoof(hello world) is %ld\n",buff,sizeof("hello world"));
    return 0;
}

the hello world
 an the sizeoof(hello world) is 12

在文件的写入中如果没有好的注意到这些小的问题会导致文件没有按自己的格式写入,接着导致文件的读取错误 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
snprintf函数是一个可变参数函数,其功能是将格式化后的字符串写入到一个字符数组中。它的函数原型是int snprintf(char* dest_str, size_t size, const char* format, ...)。 该函数有两种情况: 1. 如果格式化后的字符串长度小于size,那么整个格式化后的字符串将被复制到dest_str中,并在末尾添加一个字符串结束符'\0'。这个函数会返回欲写入的字符串长度。 2. 如果格式化后的字符串长度大于等于size,那么只会将前size-1个字符复制到dest_str中,并在末尾添加一个字符串结束符'\0',函数同样会返回欲写入的字符串长度。 需要注意的是,snprintf函数并不是标准C/C++中规定的函数,但在许多编译器中厂商提供了其实现的版本。在gcc中,该函数名称就是snprintf,在VC中则称为_snprintf。如果在VC中使用snprintf函数,会提示此函数未声明,可以将其改为_snprintf即可。 与sprintf函数不同的是,snprintf函数可以避免发生缓冲区溢出的问题,因为它可以指定写入的字符数组的大小,当格式化后的字符串长度超过设定的大小时,snprintf函数会自动截断字符串。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [c语言snprintf函数简介](https://blog.csdn.net/weixin_33701564/article/details/93488346)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* *3* [snprintf( )函数简介](https://blog.csdn.net/sk983671939/article/details/79722680)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

潘多拉的面

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值