首页 > 编程知识 正文

c语言snprintfC C snprintf–使用指南,c语语言float使用

时间:2023-05-03 15:04:23 阅读:259306 作者:4720

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 buffern"); return -1; } printf("Buffer written successfully!nNumber of characters read: %dnContent of buffer: %sn", num_read, buffer); return 0;}

Output

输出量

Buffer written successfully!Number of characters read: 33Content 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 buffern"); return -1; } printf("Buffer written successfully! Number of characters read: %d, Content of buffer: %sn", 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(), with 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 list of C++ articles.

有关C ++的类似文章,请仔细阅读我们的C ++文章列表 。

参考资料 (References) cplusplus.com article on snprintf()

关于snprintf()的cplusplus.com文章



翻译自: https://www.journaldev.com/37157/snprintf-c-plus-plus

c语言snprintf

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。