我得到了将其添加到文件路径的用户ID。但是在创建文件时遇到了麻烦。如何将用户ID添加到文件路径?我曾经用过,strcpy
但这似乎行不通。这是我的代码。
mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
register struct passwd *pw;
register uid_t uid;
uid = geteuid ();
pw = getpwuid (uid);
char str[1000];
strcpy(str, "/home/" );
strcpy(str, pw->pw_name );
strcpy(str, "/Documents/test.txt" );
int openFile = creat(str, mode);
是strcpy()的三倍?也许你想要:
strcpy(str, "/home/");
strcat(str, pw->pw_name);
strcat(str, "/Documents/test.txt");
?甚至更好:
int ret;
ret = snprintf(str, sizeof str, "%s/%s/%s"
, "/home" , pw->pw_name, "Documents/test.txt");
if (ret >= sizeof str) {... error...}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句