我的程序应该添加100个用户和密码。然而,我有困难(我将发布输出和输入文件)。应该发生的事情是用户名是从文件中提取的,并且密码也是这样(来自他们的ID号)。格式将像用户:jacob密码:se-123456。但是,似乎用户不存在。下面是我的程序:如何添加和删除输入文件中的用户到linux系统?
#include
#include
#include
#include
using namespace std;
int main()
{
cout << "Hello world." << endl;
//system("mkdir user1");
string anyCommand="", name, userpassword;
ifstream myfile ("userNames1000.txt");
if (myfile.is_open())
//code to open a file
{
while (getline (myfile,anyCommand))
{
for(int i = 0; i <= 100; i++)
{
stringstream temp;
temp << i;
name = "user";
name += temp.str();
//useradd vs. userdel
anyCommand = "useradd " + name;
cout << anyCommand << endl;
//system(anyCommand.c_str());
anyCommand = "";
userpassword = "se- " ;
//set the command, e.g., system("echo john:se-2014 |chpasswd");
anyCommand = "echo " + name + ":" + userpassword + " |chpasswd";
cout << anyCommand << endl;
system(anyCommand.c_str());
}
myfile.close();
}
}
else cout << "Unable to open file";
return 0;
}
这里的是怎么回事,当我运行程序上:
Hello World
useradd user0
echo user0:se- | chpasswd
chpasswd: line 1: user 'user0' does not exist
chpasswd: error detected, changes ignored
useradd user1
echo user1:se- | chpasswd
chpasswd: line 1: user 'user1' does not exist
chpasswd: error detected, changes ignored
useradd user2
echo user2:se- | chpasswd
chpasswd: line 1: user 'user2' does not exist
chpasswd: error detected, changes ignored
便去,直到它达到100;同样的错误继续下去。我有一种感觉,我正朝着正确的方向前进,我需要一些帮助来解决我需要解决的问题。
下面是输入文件内容的示例。
Mary:20153
Lindsey:19396
Ashley:17151
Jason:16861
2016-04-25
iuliko
+0
由于日志表示您尝试更改不存在用户的密码,因此如果您希望实际添加新用户,则应该使用[useradd](http://linux.die.net/man/8/useradd)用户 –