I got the user id to add it to the file path. But am having trouble creating the file. How do I add the user id to the file path? I used strcpy
but that does not seem to work. Here is my code.
我获得了用户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);
2 个解决方案
#1
4
Three times strcpy() ? maybe you wanted:
三次strcpy()?也许你想要:
strcpy(str, "/home/");
strcat(str, pw->pw_name);
strcat(str, "/Documents/test.txt");
? Or even better:
?甚至更好:
int ret;
ret = snprintf(str, sizeof str, "%s/%s/%s"
, "/home" , pw->pw_name, "Documents/test.txt");
if (ret >= sizeof str) {... error...}
#2
1
This is a fine purpose for snprintf (in stdio.h). One line:
这是snprintf的一个很好的目的(在stdio.h中)。一条线:
snprintf(str, 1000, "/home/%s/Documents/test.txt", pw->pw_name);
Even better to first verify that pw->pw_name is not null prior.
最好先验证pw-> pw_name先前是否为null。
The reason that your multiple strcpy does not work is that you write to the same location in memory at each call.
您的多个strcpy不起作用的原因是您在每次调用时写入内存中的相同位置。
I would not advise that you do this, but you could use strcpy provided you updated the pointer after each call. One example:
我不建议您这样做,但如果您在每次调用后更新指针,则可以使用strcpy。一个例子:
char *loc = str;
strcpy(loc, "/home/" );
loc += strlen("/home/");
strcpy(loc, pw->pw_name );
loc += strlen(pw->pw_name);
strcpy(loc, "/Documents/test.txt" );
This, however, would be an issue had you chosen a small buffer (shorter than the combined number of characters of all three strings + one more for the terminating null) — a buffer overflow.
但是,如果您选择了一个小缓冲区(短于所有三个字符串的字符组合数,再多一个用于终止空值),这将是一个问题 - 缓冲区溢出。
snprintf gives the benefit of ensuring that you don't exceed that bound:
snprintf提供了确保您不超过该范围的好处:
The functions snprintf() and vsnprintf() do not write more than size bytes (including the terminating null byte ('\0')).
函数snprintf()和vsnprintf()写入的字节数不超过大小(包括终止空字节('\ 0'))。
#1
4
Three times strcpy() ? maybe you wanted:
三次strcpy()?也许你想要:
strcpy(str, "/home/");
strcat(str, pw->pw_name);
strcat(str, "/Documents/test.txt");
? Or even better:
?甚至更好:
int ret;
ret = snprintf(str, sizeof str, "%s/%s/%s"
, "/home" , pw->pw_name, "Documents/test.txt");
if (ret >= sizeof str) {... error...}
#2
1
This is a fine purpose for snprintf (in stdio.h). One line:
这是snprintf的一个很好的目的(在stdio.h中)。一条线:
snprintf(str, 1000, "/home/%s/Documents/test.txt", pw->pw_name);
Even better to first verify that pw->pw_name is not null prior.
最好先验证pw-> pw_name先前是否为null。
The reason that your multiple strcpy does not work is that you write to the same location in memory at each call.
您的多个strcpy不起作用的原因是您在每次调用时写入内存中的相同位置。
I would not advise that you do this, but you could use strcpy provided you updated the pointer after each call. One example:
我不建议您这样做,但如果您在每次调用后更新指针,则可以使用strcpy。一个例子:
char *loc = str;
strcpy(loc, "/home/" );
loc += strlen("/home/");
strcpy(loc, pw->pw_name );
loc += strlen(pw->pw_name);
strcpy(loc, "/Documents/test.txt" );
This, however, would be an issue had you chosen a small buffer (shorter than the combined number of characters of all three strings + one more for the terminating null) — a buffer overflow.
但是,如果您选择了一个小缓冲区(短于所有三个字符串的字符组合数,再多一个用于终止空值),这将是一个问题 - 缓冲区溢出。
snprintf gives the benefit of ensuring that you don't exceed that bound:
snprintf提供了确保您不超过该范围的好处:
The functions snprintf() and vsnprintf() do not write more than size bytes (including the terminating null byte ('\0')).
函数snprintf()和vsnprintf()写入的字节数不超过大小(包括终止空字节('\ 0'))。