我正在尝试使用snprintf基于我读过的手册的函数是<stdio.h>标题的一部分,但是我收到一个错误,它被隐式声明。这是我的代码:
#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; …Run Code Online (Sandbox Code Playgroud) 我想在不使用 time.h 库的情况下重复生成随机数。我看到另一篇关于使用
\n\nsrand(getpid()); \nRun Code Online (Sandbox Code Playgroud)\n\n但这似乎对我不起作用 getpid 尚未声明。这是因为我缺少它的图书馆吗?如果是的话,我需要弄清楚如何随机生成数字,而不使用除我当前拥有的库之外的任何其他库。
\n\n#include <stdio.h>\n#include <stdlib.h>\n\n\nint main(void) {\n int minute, hour, day, month, year;\n srand(getpid());\n minute = rand() % (59 + 1 - 0) + 0;\n hour = rand() % (23 + 1 - 0) + 0;\n day = rand() % (31 + 1 - 1) + 1;\n month = rand() % (12 + 1 - 1) + 1;\n year = 2018;\n\n printf("Transferred successfully at %02d:%02d on %02d/%02d/%d\\n", hour, \n minute, day, …Run Code Online (Sandbox Code Playgroud)