snprintf函数的使用(Windows和Linux下的区别)[转] - Something TO DO

snprintf函数的使用(Windows和Linux下的区别)[转]

Something TO DO posted @ 2017年10月15日 21:58 in C++ with tags _snprintf snprintf , 2386 阅读

在C语言中,printf表示把字符串格式化输出到终端。sprintf表示把字符串格式化输出到另一个字符串。而snprintf表示把字符串格式化之后,输出指定位数到新的字符串。

 

我们看下面一段代码。

#include <stdio.h>

#define COPY_LENGTH 5

int main() {
    char buff[100] = {0};
    int result = snprintf(buff, COPY_LENGTH, "ABCDEF");
    printf("%d, %s\n", result, buff);
}

我在Windows环境编译执行(我的编译环境是:Win7x64 ,gcc 4.8.3),执行结果为-1, ABCDE。
然后把这段代码放到Ubuntu里编译执行(我的编译环境是:Ubuntu Desktop 10.04,gcc 4.8.2),执行结果为6, ABCD。

在网上找了一下原因,在Linux C语言里其定义应该如下:

int snprintf(char *restrict buf, size_t n, const char * restrict   format, ...);

函数说明:最多从源串中拷贝 n - 1 个字符到目标串中,然后再在后面加一个 0 。所以如果目标串的大小为 n 的话,将不会溢出。
函数返回值:若成功则返回欲写入的字符串长度,若出错则返回负值。

但在Windows平台,用snprintf只是引用_snprintf函数而已。_snprintf的定义如下:

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

函数说明:
如果格式化后的字符串(源串)长度小于或者等于count,则拷贝源串到buffer,并返回源串长度。
如果格式化后的字符串(源串)长度大于count,则拷贝源串的前count个字符到buffer,并返回一个负数。

所以说,_snprintf函数最多可以比源串中拷贝 n 个字符到目标串中,有可能不会在后面加0。所以会出现上面的问题。如果想要使两者相同,可以在代码中直接把最后一位设置成0,这样两个平台就是一样的结果了。当然在实际应用中,可能根据要实现的功能,如何设置要自己判断。

比如,如果只是把最后一位设置为0,上面的代码可以改成:

#include <stdio.h>

#define COPY_LENGTH 5

int main() {
    char buff[100] = {0};
    snprintf(buff, COPY_LENGTH, "ABCDE");
    buff[COPY_LENGTH - 1] = 0;
    printf("%s\n", buff);
}
Avatar_small
How to Disable Lapto 说:
2022年8月10日 01:14

It happens many times that you want to use your external keyboard but struggle to have the enabled keys of your laptop. Unlike in desktop it is not a physical cord that you can unplug in order to disable the keyboard. How to Disable Laptop Keyboard Another way to disable the cordless keyboard by toggle off the Bluetooth on computer to disconnect the keyboard. Where as in laptop the keyboard comes as in-built with the device. In any circumstances you’re free to go ahead and disable the Laptop Keyboard and start using your external one.

Avatar_small
CGBSE Question Paper 说:
2022年9月05日 19:51

CG Board Model Paper 2023 Class 10 Pdf Download with Answers for English Medium, Hindi Medium, Urdu Medium & Students for Small Answers, Long Answer, Very Long Answer Questions, and Essay Type Questions to Term1 & Term2 Exams at official website. CGBSE Question Paper Class 11 New Exam Scheme or Question Pattern for Sammittive Assignment Exams (SA1 & SA2): Very Long Answer (VLA), Long Answer (LA), Small Answer (SA), Very Small Answer (VSA), Single Answer, Multiple Choice and etc.

Avatar_small
Archive Amazon Order 说:
2022年12月20日 21:33

There is no specific button or option on Amazon that allows you to totally remove purchases from your order history. However, you may archive an order to prevent it from appearing in your order history. Archive Amazon Order History This simple guide will explain how to delete Amazon purchases order history on Android, iPhone, and PC in order to erase or remove them.

Avatar_small
2nd Inter Question P 说:
2023年2月14日 23:32

Board of Secondary Education has deferred the Class 12th board Exam should be held from May 2024. The candidates can check the Class 12th Board Model Question Papers from true site. 2nd Inter Question Paper 2024 The 12th Question Paper 2024 will likewise be reported on class 12th students can take note of the total 12th Question Paper 2024.


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter
Host by is-Programmer.com | Power by Chito 1.3.3 beta | © 2007 LinuxGem | Design by Matthew "Agent Spork" McGee