- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我正在编写一个 C 程序,预计可以使用所有主要编译器进行编译。目前我正在 linux 机器上的 GCC 上开发,将在提交代码之前在 MSVC 上编译。为了简化交叉编译,我使用 -ansi
和 -pedantic
标志进行编译。在我开始使用 C89 标准中不可用的 snprintf
之前,这一直很有效。 GCC 可以在没有 -ansi
开关的情况下编译它,但是 MSVC 总是会失败,因为它不支持 C99。
所以我做了类似的事情,
#ifdef WIN32
#define snprintf sprintf_s
#endif
这很有效,因为 snprintf
和 sprintf_s
具有相同的签名。我想知道这是正确的方法吗?
最佳答案
我找到了 this关于使用 _snprintf()
作为替代方案,以及缓冲区溢出保护实际触发时涉及的问题。从我快速浏览的内容来看,类似的警告适用于 sprintf_s
。
Can you see the problem? In the Linux version, the output is always null-terminated. In MSVC, it's not.
Even more subtle is the difference between the
size
parameter in Linux andcount
parameter in MSVC. The former is the size of the output buffer including the terminating null and the latter is the maximum count of characters to store, which excludes the terminating null.
哦,别忘了给微软发邮件,要求他们支持当前的语言标准。 (我知道他们已经宣布他们没有支持 C99 的计划,但无论如何都要打扰他们。他们活该。)
底线,如果你想真正安全地玩它,你必须提供你自己的 snprintf()
(_snprintf()
或 sprintf_s()
捕获它们的非标准行为)用于 MSVC。
关于c - 在跨平台应用程序中使用 snprintf,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3976306/
在我从事的项目(一个 Windows 和 Ubuntu 应用程序)中,我定义了一个与日志库交互的宏。本质上,这个宏接受类似 printf 的输入,转换为字符串,并将其发送到日志库。我附上一个使用 st
我正在尝试使用 snprintf基于我读过的手册的函数是 的一部分header 但是我收到一个错误,它已被隐式声明。这是我的代码: #include #include #include str
当我通过 Fortify 分析器工具之一运行我的代码时,它提示 snprintf 说“格式字符串参数没有正确限制函数可以写入的数据量” 我了解 snprintf 不应导致缓冲区溢出。但仍然是为什么该工
在我的代码中,我使用 snprintf 如下所示,并且能够看到以下行为 char text[30] = {0}; snprintf(text, sizeof(text), "%s", "hello")
我创建了以下程序: #include #include #define TESTER "name=%s " int main(){ char *x; x = malloc(100);
当我通过 Fortify 分析器工具之一运行我的代码时,它提示 snprintf 说“格式字符串参数没有正确限制函数可以写入的数据量” 我了解 snprintf 不应导致缓冲区溢出。但仍然是为什么该工
我收到 format not a string literal and no format arguments当我在 Linux 上编译它时发出警告。 snprintf显示 const char*对于
char symbols[16] = ""; int index = 0; while (1) { if (index % 2) snprintf(symbols, siz
我正在尝试了解 snprintf 并找到了 this answer举个例子: char buf[20] = ""; char *cur = buf, * const end = buf + sizeo
已关闭。此问题需要 debugging details 。目前不接受答案。 编辑问题以包含 desired behavior, a specific problem or error, and the
我正在尝试理解 C 中的 char 指针。基本上我正在做的是声明一个 char* 指针 main 并在另一个函数中通过引用传递并在那里进行修改。现在,我想打印 main 中 char 的值,这给了我段
我正在使用 C 语言(指针、指针-指针等,我喜欢它)做我的第一步,所以如果这是一个愚蠢的问题,请仁慈。 该片段不输出任何内容: char buf[256]; snprintf(buf, sizeof
我需要将一些数字(整数和 double )转换为 std::strings,并且出于性能原因不能使用 stringstream(我们发现它在同时使用时非常慢) 如果能做这样的事情就好了 templat
我已经查看这段代码一段时间了,但似乎找不到我的错误。我想我知道 snprintf(dest, n, fmt, src) 应该如何工作(它在填充 后返回 src 缓冲区中剩余的字符数>dest 缓冲区与
为什么 snprintf 在第二种情况下给出不同的值。是不是因为任何整数限制。您能否解释一下 snprintf 的工作原理以及为什么会出现负值 #include #include main() {
考虑以下代码: char *myContent = "content"; int size = snprintf(NULL, 0, "INSERT INTO myTable (col1) VALUES
我正在编写自己的 ncurses 库,突然发现在 GDB 中 snprintf() 返回的长度大于我指定的长度。这是定义的行为还是我的一些错误? (可重现的)片段代码是这样的: niko: snipp
我写了一个简单的小程序来使用 snprintf 读取具有以下格式的文件, skip first 15 chars , next 9 chars are sequence number, next 2
我使用snprintf 将格式化的数据写入磁盘,但我有一个问题,如何将它保存到用户的主目录? snprintf(filename, sizeof(filename), "%s_%s.sho", cl
定义: CHAR_BACKSLASH 定义为 '\\' 或 0x5C 变量: workingDir 是一个 C 字符串 myFilePath 是一个 C 字符串 int len = strlen(wo
我是一名优秀的程序员,十分优秀!