c语⾔snprintf_CC++snprintf()–使⽤指南
c语⾔snprintf
In this article, we’ll understand the working and the usage of the snprintf() C++ function.
在本⽂中,我们将了解snprintf() C ++函数的⼯作和⽤法。
The snprintf() function is used to write formatted output to a string. Let’s understand how we can use this function, by showing it’s syntax and giving examples.
snprintf()函数⽤于将格式化的输出写⼊字符串。 通过显⽰其语法并给出⽰例,让我们了解如何使⽤此函数。
C / C ++中snprintf()的基本语法 (Basic Syntax of snprintf() in C/C++)
Since the snprintf() function will write to a string buffer, it takes a string as an argument, and also a formatted string.
由于snprintf()函数将写⼊字符串缓冲区,因此它将字符串作为参数,还有格式化的字符串。
A formatted string is any string that contains format specifiers, like %d, %c or %s. This is similar to printf() or cout, except that it will write to a string instead.
格式化的字符串是任何包含格式说明符的字符串,例如%d , %c或%s 。 这类似于printf()或cout ,除了它将写⼊⼀个字符串。
The basic function signature is the following:
基本功能签名如下:
int snprintf (char* buffer, size_t buf_size, const char* format);
Here, buffer is the string buffer that we will write to, with a maximum capacity of buf_size. format is the format string, that we will write to the buffer.
在这⾥, buffer是我们将写⼊的字符串缓冲区,最⼤容量为buf_size 。 format是我们将写⼊缓冲区的格式字符串。
NOTE: The buffer is an array of characters (char*), and not a string. This is because this is a C compatible function, and C does not have the string class.
注意 :缓冲区是⼀个字符数组( char* ),⽽不是string 。 这是因为这是C兼容函数,并且C没有string类。
If the execution is successful, it will return the number of characters written, if there is no problem in writing. Otherwise, it will return a negative integer.
如果执⾏成功,如果没有问题,它将返回已写⼊的字符数。 否则,它将返回⼀个负整数。
If the buffer size is too small, the input string will be truncated to the buffer size.
如果缓冲区⼤⼩太⼩,则输⼊字符串将被截断为缓冲区⼤⼩。
Similar to printf(), this is a library function defined in <stdio.h>
与printf()类似,这是在<stdio.h>定义的库函数
#include <stdio.h>
int snprintf (char* buffer, size_t buf_size, const char * format);
Let’s now take a look at some examples of this function.
现在让我们看⼀下此函数的⼀些⽰例。
在C / C ++中使⽤snprintf() (Using snprintf() in C/C++)
We’ll take a format string containing some integers and strings, and write it to a buffer.
我们将使⽤包含⼀些整数和字符串的格式字符串,并将其写⼊缓冲区。
Let’s assume a format string of the form:
假设格式字符串的格式为:
Hello %s, your roll number is %d.
We’ll now write it to a buffer, using snprintf().
现在,我们将使⽤snprintf()将其写⼊缓冲区。
#include <stdio.h>
int main() {
/
/ Allocate stack memory for our buffer
char buffer[256];
char name[20] = "Amit";
int num_read = snprintf(buffer, sizeof(buffer), "Hello %s, your roll number is %d", name, 10);
if (num_read < 0) {
fprintf(stderr, "Error while writing to buffer\n");c++中string的用法
return -1;
}
printf("Buffer written successfully!\nNumber of characters read: %d\nContent of buffer: %s\n", num_read, buffer);
return 0;
}
Output
输出量
Buffer written successfully!
Number of characters read: 33
Content of buffer: Hello Amit, your roll number is 10
As you can see, our buffer was indeed updated with our format string contents!
如您所见,我们的缓冲区确实已使⽤我们的格式字符串内容进⾏了更新!
Let’s take another case when you’re trying to write a string that is too large for our buffer. Here, let’s take a small buffer size of 20, and try to see what happens.
当您尝试编写⼀个对于我们的缓冲区来说太⼤的字符串时,让我们采取另⼀种情况。 在这⾥,让我们将缓冲区的⼤⼩设为20 ,然后尝试看看会发⽣什么。
#include <stdio.h>
int main() {
// Allocate stack memory for our buffer
char buffer[20];
char name[20] = "Amit";
int num_read = snprintf(buffer, sizeof(buffer), "Hello %s, your roll number is %d", name, 10);
if (num_read < 0) {
fprintf(stderr, "Error while writing to buffer\n");
return -1;
}
printf("Buffer written successfully! Number of characters read: %d, Content of buffer: %s\n", num_read, buffer);
return 0;
}
Output
输出量
Buffer written successfully! Number of characters read: 34, Content of buffer: Hello Amit, your ro
Even though we’ve read the same number of characters as before (34), since our buffer size is not big enough, the format string is reduced to the buffer size.
即使我们已经读取了与之前相同的字符数(34),由于我们的缓冲区⼤⼩不够⼤,因此格式字符串会减⼩为缓冲区⼤⼩。
So, our output only has 20 characters, the same as the size of the buffer.
因此,我们的输出仅包含20字符,与缓冲区的⼤⼩相同。
结论 (Conclusion)
Hopefully, you’ve now understood how you can use the snprintf() function. This is similar to printf(), w
ith the difference being that you write to a string buffer, instead of stdout.
希望您现在已经了解如何使⽤snprintf()函数。 这类似于printf() ,不同之处在于您写⼊字符串缓冲区⽽不是stdout 。
For similar articles on C++, do go through our .
有关C ++的类似⽂章,请仔细阅读我们 。
参考资料 (References)
on snprintf()
关于snprintf()的
翻译⾃:
c语⾔snprintf
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论