我可能稍后将其更改为更好的数据库形式,如果您对此有任何建议然后拍摄,我对C ++很陌生,我现在正在考虑使用JSON,因为我在Python中使用了它。现在虽然我只是想测试并确保一切正常。
这是我的问题,早些时候我创建了一个函数来创建一个存储帐户信息的目录。现在我正在将密码存储在帐户目录中。这是我得到的:
cout<<"Type 'NEW' to create an account or enter your account name.\n";
cout<<"Account Name: ";
cin>> account;
cin.ignore();
if (account == "New" or account == "NEW" or account == "new"){
string accname;
cout<<" \n";
cout<<"Please enter your desired Account Name: ";
cin>> accname;
cin.ignore();
if (accname.length() > 2){
std::string yesOrno;
cout<<" \n";
cout<<"You have chosen, '" << accname << "' as your Account Name, correct? ";
cin>> yesOrno;
cin.ignore();
if (yesOrno == "Yes" or yesOrno == "YES" or yesOrno == "yes" or yesOrno == "y"){
system(("mkdir -p /home/user/Program/accounts/"+accname).c_str());
cout<<" \n";
cout<<"It is advised to select a strong password for your account. Such as a phrase ins\n";
cout<<"tead of a word and/or using special characters, numbers, and letters. We require\n";
cout<<"it to be at least six characters long for security reasons.\n";
cout<<" \n";
std::string str1;
std::string str2;
cout<<"Please select a password: ";
cin>> str1;
cin.ignore();
cout<<"Please retype your password: ";
cin>> str2;
cin.ignore();
if (str1 == str2){
ofstream password;
password.open("/home/user/Program/accounts/"+accname+"password.txt");
password.close();
}
else {
cout<< "Passwords do not match.";
}
}
}
else {
cout<<"That is too short, please choose another Account Name: ";
}
}
现在的问题是,如上所述,我在编译时遇到错误:
intro.cpp: In function ‘int main()’:
intro.cpp:66:72: error: no matching function for call to ‘std::basic_ofstream<char>::open(std::basic_string<char>)’
password.open("/home/user/Program/accounts/"+accname+"password.txt");
^
intro.cpp:66:72: note: candidate is:
In file included from intro.cpp:2:0:
/usr/include/c++/4.8/fstream:713:7: note: void std::basic_ofstream<_CharT, _Traits>::open(const char*, std::ios_base::openmode) [with _CharT = char; _Traits = std::char_traits<char>; std::ios_base::openmode = std::_Ios_Openmode]
open(const char* __s,
^
/usr/include/c++/4.8/fstream:713:7: note: no known conversion for argument 1 from ‘std::basic_string<char>’ to ‘const char*’
我不知道这意味着什么。
然后我尝试了密码部分:
password.open("/home/user/Program/accounts/password.txt");
这样可以编译和创建.txt,但它并没有专门创建我想要的文件。有没有办法做到这一点?我是在正确的轨道上吗?
答案 0 :(得分:0)
您已经在此处找到了编译器错误的解决方案:system(("mkdir -p /home/user/Program/accounts/"+accname).c_str());
尝试:password.open(("/home/user/Program/accounts/"+accname+"password.txt").c_str());
最近的编译器会将一个字符串作为参数打开,但旧的编译器需要一个字符数组。也许你有一个最近的编译器,它只是被字符串追加混淆。不能肯定地说,但如果再次使用c_str(),你应该好好去。
答案 1 :(得分:0)
这里的问题是std :: basic_ofstream :: open没有重载接受std :: string,在C ++ 03 中。但是,在 C ++ 11 中,open的重载是:
void open( const char *filename,
ios_base::openmode mode = ios_base::out );
void open( const std::string &filename,
ios_base::openmode mode = ios_base::out );
因此,如果在构建代码时将-std = c ++ 11添加到编译器命令行,原始代码应该可以正常工作。
另一个问题是你错过了斜线。这是您应该使用专用库来操作文件系统对象的原因之一,例如Boost.Filesystem。
在您添加accname:
之后,正好缺少斜杠password.open(("/home/user/Program/accounts/"+accname+"/password.txt").c_str());
再次,如果你添加-std = c ++ 11,那么你不需要在这里使用c_str(),例如修复变为:
password.open("/home/user/Program/accounts/"+accname+"/password.txt");