所以我得到了分配,以使用C从Linux中的/ etc / passwd文件中提取名称。
我应该使用strtok命令仅获取:usename,名字(中间名)和姓氏。
struct和main的简单部分已经完成,我遇到的麻烦是strtok部分,在该部分中,我必须提取信息并编写代码中所见的部分。
有什么解决的建议吗?
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
struct useracc {
char username[100];
char firstname [250]; // All names except last name incl. middle name
char lastname [100]; // Surname
};
typedef struct useracc acc_t;
void read_data (acc_t *userarray);
void write_data (acc_t *userarray);
void read_and_write_eks();
int main() {
acc_t userarray[200];
read_and_write_eks();
read_data (userarray);
write_data(userarray);
return 0;
}
void read_data (acc_t *userarray) {
// This is what I need help for
}
void write_data (acc_t *userarray) {
// This is what I need help for
}
最佳答案
好的,经过一些尝试,我得到了可以工作的代码。但是我仍然无法打印出想要的特定部分,例如uid和名称。我还需要使其具有中间名的人在带有名字的选项卡中获得中间名。
关于如何使用strtok打印特定零件的任何建议?
到目前为止,这是我的代码:
enter code here
无效的read_data(acc_t * userarray){
char line[256], words[20];
char *sp, *uname, *fname, *lname;
int uid, bindex=0, index;
FILE*fp = fopen("/etc/passwd","r");
while(fgets(line,256,fp))
{
sp = strtok(line, ", : / ");
while (sp != NULL)
{
printf("[%s]\n ", sp);
sp = strtok(NULL, ", : /");
}
uid++;
}
关于c -/etc/passwd的C程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58147614/