gpt4 book ai didi

c - snprintf 函数未声明?

转载 作者:行者123 更新时间:2023-12-04 11:02:51 25 4
gpt4 key购买 nike

我正在尝试使用 snprintf基于我读过的手册的函数是 <stdio.h> 的一部分header 但是我收到一个错误,它已被隐式声明。这是我的代码:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct users {
char* user_id;
};
typedef struct users users_t;

int save_user_detail(user_t);

int main() {
users_t users;
save_user_detail(users);
return 0;
}

int save_user_detail(users_t users)
{

printf("Type the filename = ");
scanf("%s", users.user_id);
char* extension = ".txt";
char fileSpec[strlen(users.user_id)+strlen(extension)+1];
FILE *file;
snprintf(fileSpec, sizeof(fileSpec), "%s%s", users.user_id, extension);
file = fopen(fileSpec, "w");
if(file==NULL)
{
printf("Error: can't open file.\n");
return 1;
}
else
{
printf("File written successfully.\n");
fprintf(file, "WORKS!\r\n");
}
fclose(file);
return 0;
}

Error

最佳答案

您似乎在使用 gcc ,但是这个编译器不一定使用glibc,它符合C标准,支持snprintf .

在 Windows 架构上,您可能正在使用 Microsoft C 库,在旧版本中没有 snprintf或重命名为_snprintf .

您可以通过以下 2 种方法尝试解决您的问题:

  • 尝试使用 _snprintf而不是 snprintf .
  • 定义 snprintf包含 <stdio.h> 后手 Action 为

    int snprintf(char *buf, size_t size, const char *fmt, ...);

如果运行时库确实有 snprintf 的符号,编译器应该停止提示缺少原型(prototype)。使用匹配的调用约定,它将链接到它并且程序应该按预期运行。

关于c - snprintf 函数未声明?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50386828/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com