vsnprintf() 将可变参数格式化输出到字符数组

vsnprintf{} 将可变参数格式化输出到一个字符数组

  • 1. function `vsnprintf()`
    • 1.1. `const int num_bytes = vsnprintf(NULL, 0, format, arg);`
  • 2. Parameters
  • 3. Return value
  • 4. Example
  • 5. llama.cpp
  • References

1. function vsnprintf()

https://cplusplus.com/reference/cstdio/vsnprintf/

int vsnprintf(char *target_str, size_t n, const char *format, va_list arg);

Write formatted data from variable argument list to sized buffer

Composes a string with the same text that would be printed if format was used on printf, but using the elements in the variable argument list identified by arg instead of additional function arguments and storing the resulting content as a C string in the buffer pointed by target_str (taking n as the maximum buffer capacity to fill).
使用在 printf 上使用 format 时打印的相同文本组成一个字符串,但使用 arg 标识的变量参数列表中的元素而不是额外的函数参数,并将结果内容作为 C 字符串存储在 target_str 指向的缓冲区中 (以 n 作为要填充的最大缓冲区容量)。

If the resulting string would be longer than n - 1 characters, the remaining characters are discarded and not stored, but counted for the value returned by the function.
如果结果字符串长度超过 n - 1 个字符,则剩余字符将被丢弃并且不被存储,但计入函数返回的值。

#include <stdarg.h>
#include <stdio.h>void formatted_data(char *str, char *fmt, ...);int main(void) {char str[256];formatted_data(str, "%s-%s-%s\n", "Yong", "Qiang", "Cheng");printf("The string: %s\n", str);return 0;
}void formatted_data(char *str, char *fmt, ...) {va_list arg_ptr;va_start(arg_ptr, fmt);const int num_bytes = vsnprintf(str, 10, fmt, arg_ptr);va_end(arg_ptr);printf("The number of bytes: %d\n", num_bytes);
}
The number of bytes: 17
The string: Yong-Qian
请按任意键继续. . .

Internally, the function retrieves arguments from the list identified by arg as if va_arg() was used on it, and thus the state of arg is likely to be altered by the call.
在内部,该函数从 arg 标识的列表中检索参数,就好像在其上使用 va_arg() 一样,因此 arg 的状态可能会因调用而改变。

In any case, arg should have been initialized by va_start() at some point before the call, and it is expected to be released by va_end() at some point after the call.
无论如何,arg 应该在调用之前的某个时间点由 va_start() 初始化,并且预计在调用之后的某个时间点由 va_end() 释放。

1.1. const int num_bytes = vsnprintf(NULL, 0, format, arg);

int vsnprintf(char *target_str, size_t n, const char *format, va_list arg);const int num_bytes = vsnprintf(NULL, 0, format, arg);

At most n - 1 characters are written. The resulting character string will be terminated with a null character, unless n is zero.
最多写入 n - 1 个字符。除非 n 为零,否则生成的字符串将以空字符 (\0) 结尾。

If n is zero, nothing is written and target_str may be a null pointer, however the return value (number of bytes that would be written not including the null terminator) is still calculated and returned.
如果 n 为零,则不会写入任何内容,并且 target_str 可能为空指针,但是仍会计算并返回值 (不包括空终止符的写入字节数)。

#include <iostream>
#include <cstdarg>
#include <vector>std::string string_format(const char *format, ...) {va_list ap1;va_list ap2;va_start(ap1, format);va_copy(ap2, ap1);const int size1 = vsnprintf(NULL, 0, format, ap1);std::vector<char> buf(size1 + 1);const int size2 = vsnprintf(buf.data(), size1 + 1, format, ap2);std::cout << "size1=" << size1 << ", size2=" << size2 << std::endl;std::cout << "buf: " << buf.data() << std::endl;va_end(ap2);va_end(ap1);return std::string(buf.data(), size1);
}int main() {const std::string batch = string_format("logical maximum batch size (default: %d)", 9);std::cout << batch.c_str() << std::endl << std::endl;const std::string ubatch = string_format("physical maximum batch size (default: %d)", 16);std::cout << ubatch.c_str() << std::endl;return 0;
}

在这里插入图片描述

2. Parameters

  • target_str

Pointer to a buffer where the resulting C-string is stored.

The buffer should have a size of at least n characters.

  • n

Maximum number of bytes to be used in the buffer.

The generated string has a length of at most n - 1, leaving space for the additional terminating null character.
生成的字符串的长度最多为 n - 1,为额外的终止空字符留出空间。vsnprintf() 要在结果的末尾追加 \0

size_t is an unsigned integral type.

  • format

C string that contains a format string that follows the same specifications as format in printf (see printf for details).

  • arg

A value identifying a variable arguments list initialized with va_start().

va_list is a special type defined in <cstdarg>.

3. Return value

The vsnprintf() function returns the number of bytes that are written in the array, not counting the ending null character.
vsnprintf() 函数返回写入数组的字节数,不计算结尾的空字符。

The number of characters that would have been written if n had been sufficiently large, not counting the terminating null character.
写入的字符数,不包括终止空字符。

If an encoding error occurs, a negative number is returned.

Notice that only when this returned value is non-negative and less than n, the string has been completely written.
注意,只有当这个返回值非负且小于 n 时,字符串才被完全写入。

#include <stdarg.h>
#include <stdio.h>void formatted_data(char *str, char *fmt, ...);int main(void) {char str[256];formatted_data(str, "%s-%s-%s\n", "Yong", "Qiang", "Cheng");printf("The string: %s\n", str);return 0;
}void formatted_data(char *str, char *fmt, ...) {va_list arg_ptr;va_start(arg_ptr, fmt);const int num_bytes = vsnprintf(str, 64, fmt, arg_ptr);va_end(arg_ptr);printf("The number of bytes: %d\n", num_bytes);
}
The number of bytes: 17
The string: Yong-Qiang-Cheng请按任意键继续. . .

The number of characters written if successful or negative value if an error occurred. If the resulting string gets truncated due to n limit, function returns the total number of characters (not including the terminating null-byte) which would have been written, if the limit was not imposed.
如果成功则返回写入的字符数,如果发生错误则返回负值。如果结果字符串由于 n 限制而被截断,则函数返回在未施加限制的情况下应写入的总字符数 (不包括终止空字节)。

  • 如果没有截断,则返回成功打印到 target_str 中的字符个数,字符个数不包括末尾追加的 \0
  • 如果发生截断,则返回不限制打印字符数量的情况下可以写入的字符个数,字符个数不包括末尾追加的 \0
  • 如果格式化解析失败,则返回负数。

4. Example

#ifndef _CRT_SECURE_NO_WARNINGS
#define _CRT_SECURE_NO_WARNINGS
#endif#include <stdio.h>
#include <stdarg.h>void PrintError(const char *format, ...) {char buffer[256];va_list args;va_start(args, format);const int num_bytes = vsnprintf(buffer, 256, format, args);printf("The number of bytes: %d\n", num_bytes);perror(buffer);va_end(args);
}int main() {char FileName[] = "qiang.txt";FILE *file_ptr = fopen(FileName, "r");if (NULL == file_ptr) {PrintError("Open %s", FileName);}else {// file successfully openfclose(file_ptr);}return 0;
}

在这里插入图片描述

5. llama.cpp

https://github.com/ggerganov/llama.cpp

/home/yongqiang/llm_work/llama_cpp_25_01_05/llama.cpp/src/llama-arch.cpp

#include <iostream>
#include <cstdarg>
#include <vector>std::string format(const char *fmt, ...) {va_list ap1;va_list ap2;va_start(ap1, fmt);va_copy(ap2, ap1);const int size1 = vsnprintf(NULL, 0, fmt, ap1);std::vector<char> buf(size1 + 1);const int size2 = vsnprintf(buf.data(), size1 + 1, fmt, ap2);std::cout << "size1=" << size1 << ", size2=" << size2 << std::endl;std::cout << "buf: " << buf.data() << std::endl;va_end(ap2);va_end(ap1);return std::string(buf.data(), size1);
}int main() {const std::string output_norm = format("output_norm", 1, 2);std::cout << output_norm.c_str() << std::endl << std::endl;const std::string attn_q = format("blk.%d.attn_q", 3, 4);std::cout << attn_q.c_str() << std::endl << std::endl;const std::string ffn_down = format("blk.%d.ffn_down.%d", 5, 6);std::cout << ffn_down.c_str() << std::endl << std::endl;const std::string ffn_up = format("blk.%d.ffn_up.%d", 7, 8);std::cout << ffn_up.c_str() << std::endl << std::endl;return 0;
}

在这里插入图片描述

References

[1] Yongqiang Cheng, https://yongqiang.blog.csdn.net/
[2] vsnprintf() - Print Argument Data to Buffer, https://www.ibm.com/docs/en/i/7.5?topic=functions-vsnprintf-print-argument-data-buffer

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.rhkb.cn/news/11046.html

如若内容造成侵权/违法违规/事实不符,请联系长河编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

一文大白话讲清楚webpack基本使用——17——Tree Shaking

文章目录 一文大白话讲清楚webpack基本使用——17——Tree Shaking1. 建议按文章顺序从头看&#xff0c;一看到底&#xff0c;豁然开朗2. 啥叫Tree Shaking3. 什么是死代码&#xff0c;怎么来的3. Tree Shaking的流程3.1 标记3.2 利用Terser摇起来 4. 具体使用方式4.1 适用前提…

仿真设计|基于51单片机的温湿度、一氧化碳、甲醛检测报警系统

目录 具体实现功能 设计介绍 51单片机简介 资料内容 仿真实现&#xff08;protues8.7&#xff09; 程序&#xff08;Keil5&#xff09; 全部内容 资料获取 具体实现功能 &#xff08;1&#xff09;温湿度传感器、CO传感器、甲醛传感器实时检测温湿度值、CO值和甲醛值进…

几种K8s运维管理平台对比说明

目录 深入体验**结论**对比分析表格**1. 功能对比****2. 用户界面****3. 多租户支持****4. DevOps支持** 细对比分析1. **Kuboard**2. **xkube**3. **KubeSphere**4. **Dashboard****对比总结** 深入体验 KuboardxkubeKubeSphereDashboard 结论 如果您需要一个功能全面且适合…

GenAI 在金融服务领域的应用:2025 年的重点是什么

作者&#xff1a;来自 Elastic Karen Mcdermott GenAI 不是魔法 我最近参加了 ElasticON&#xff0c;我们与纽约 Elastic 社区一起度过了一天&#xff0c;讨论了使用检索增强生成 (retrieval augmented generation - RAG) 为大型语言模型 (large language models - LLMs) 提供…

如何对系统调用进行扩展?

扩展系统调用是操作系统开发中的一个重要任务。系统调用是用户程序与操作系统内核之间的接口,允许用户程序执行内核级操作(如文件操作、进程管理、内存管理等)。扩展系统调用通常包括以下几个步骤: 一、定义新系统调用 扩展系统调用首先需要定义新的系统调用的功能。系统…

LightM-UNet(2024 CVPR)

论文标题LightM-UNet: Mamba Assists in Lightweight UNet for Medical Image Segmentation论文作者Weibin Liao, Yinghao Zhu, Xinyuan Wang, Chengwei Pan, Yasha Wang and Liantao Ma发表日期2024年01月01日GB引用> Weibin Liao, Yinghao Zhu, Xinyuan Wang, et al. Ligh…

Cubemx文件系统挂载多设备

cubumx版本&#xff1a;6.13.0 芯片&#xff1a;STM32F407VET6 在上一篇文章中介绍了Cubemx的FATFS和SD卡的配置&#xff0c;由于SD卡使用的是SDIO通讯&#xff0c;因此具体驱动不需要自己实现&#xff0c;Cubemx中就可以直接配置然后生成SDIO的驱动&#xff0c;并将SD卡驱动和…

电子电气架构 --- 汽车电子拓扑架构的演进过程

我是穿拖鞋的汉子&#xff0c;魔都中坚持长期主义的汽车电子工程师。 老规矩&#xff0c;分享一段喜欢的文字&#xff0c;避免自己成为高知识低文化的工程师&#xff1a; 简单&#xff0c;单纯&#xff0c;喜欢独处&#xff0c;独来独往&#xff0c;不易合同频过着接地气的生活…

2025 年,链上固定收益领域迈向新时代

“基于期限的债券市场崛起与Secured Finance的坚定承诺” 2025年&#xff0c;传统资产——尤其是股票和债券——大规模涌入区块链的浪潮将创造历史。BlackRock 首席执行官 Larry Fink 近期在彭博直播中表示&#xff0c;代币化股票和债券将逐步融入链上生态&#xff0c;将进一步…

数据密码解锁之DeepSeek 和其他 AI 大模型对比的神秘面纱

本篇将揭露DeepSeek 和其他 AI 大模型差异所在。 目录 ​编辑 一本篇背景&#xff1a; 二性能对比&#xff1a; 2.1训练效率&#xff1a; 2.2推理速度&#xff1a; 三语言理解与生成能力对比&#xff1a; 3.1语言理解&#xff1a; 3.2语言生成&#xff1a; 四本篇小结…

Ollama部署指南

什么是Ollama&#xff1f; Ollama是一个专为在本地机器上便捷部署和运行大型语言模型&#xff08;LLM&#xff09;而设计的开源工具。 如何部署Ollama&#xff1f; 我是使用的云平台&#xff0c;大家也可以根据自己的云平台的特点进行适当的调整。 使用系统&#xff1a;ubun…

群晖Alist套件无法挂载到群晖webdav,报错【连接被服务器拒绝】

声明&#xff1a;我不是用docker安装的 在套件中心安装矿神的Alist套件后&#xff0c;想把夸克挂载到群晖上&#xff0c;方便复制文件的&#xff0c;哪知道一直报错&#xff0c;最后发现问题出在两个地方&#xff1a; 1&#xff09;挂载的路径中&#xff0c;直接填 dav &…

Kubernetes组成及常用命令

Pods(k8s最小操作单元)ReplicaSet & Label(k8s副本集和标签)Deployments(声明式配置)Services(服务)k8s常用命令Kubernetes(简称K8s)是一个开源的容器编排系统,用于自动化应用程序的部署、扩展和管理。自2014年发布以来,K8s迅速成为容器编排领域的行业标准,被…

hexo部署到github page时,hexo d后page里面绑定的个人域名消失的问题

Hexo 部署博客到 GitHub page 后&#xff0c;可以在 setting 中的 page 中绑定自己的域名&#xff0c;但是我发现更新博客后绑定的域名消失&#xff0c;恢复原始的 githubio 的域名。 后面搜索发现需要在 repo 里面添加 CNAME 文件&#xff0c;内容为 page 里面绑定的域名&…

vim的特殊模式-可视化模式

可视化模式&#xff1a;按 v进入可视化模式 选中 y复制 d剪切/删除 可视化块模式: ctrlv 选中 y复制 d剪切/删除 示例&#xff1a; &#xff08;vim可视化模式的进阶使用&#xff1a;vim可视化模式的进阶操作-CSDN博客&#xff09;

【教程】在CMT上注册账号并声明Conflicts

转载请注明出处&#xff1a;小锋学长生活大爆炸[xfxuezhagn.cn] 如果本文帮助到了你&#xff0c;欢迎[点赞、收藏、关注]哦~ 目录 注册账号 声明冲突 账号验证 每位作者都要注册并声明冲突&#xff0c;不然会直接拒稿&#xff01; 注册账号 https://cmt3.research.microsoft…

拉格朗日定理

根号n为枚举的条件 d从c开始循环&#xff08;防止重复计算平方和&#xff09; #include<bits/stdc.h> using namespace std; using lllong long; const int N5e69;int n; int C[N],D[N];int main() {cin>>n;memset(C,-1,sizeof C);for(int c0;c*c<n;c)for(int d…

什么是线性化PDF?

线性化PDF是一种特殊的PDF文件组织方式。 总体而言&#xff0c;PDF是一种极为优雅且设计精良的格式。PDF由大量PDF对象构成&#xff0c;这些对象用于创建页面。相关信息存储在一棵二叉树中&#xff0c;该二叉树同时记录文件中每个对象的位置。因此&#xff0c;打开文件时只需加…

省级-新质生产力数据(2010-2022年)-社科数据

省级-新质生产力数据&#xff08;2010-2022年&#xff09;-社科数据https://download.csdn.net/download/paofuluolijiang/90028612 https://download.csdn.net/download/paofuluolijiang/90028612 新质生产力是指在现代科技和经济社会发展的推动下&#xff0c;由新的生产要素…

17.2 图形绘制6

版权声明&#xff1a;本文为博主原创文章&#xff0c;转载请在显著位置标明本文出处以及作者网名&#xff0c;未经作者允许不得用于商业目的。 17.2.7 Screen类 Screen类从字面上看就知道是与屏幕显示相关的&#xff0c;表示单个系统上的一个或多个显示设备。 Screen常用属性…