在变量目录中创建 .txt

creating a .txt in a variable directory

稍后我可能会将其更改为更好的数据库形式,如果您对此有任何建议,请提出建议,我是 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,但它并没有专门在我想要的地方创建文件。有没有办法做到这一点?我在正确的轨道上吗?

您已经在此处找到编译器错误的解决方案:system(("mkdir -p /home/user/Program/accounts/"+accname).c_str());

尝试:password.open(("/home/user/Program/accounts/"+accname+"password.txt").c_str());

最近的编译器会把一个字符串作为参数来打开,但是旧的需要一个字符数组。也许你有一个最新的编译器,它只是被字符串追加搞糊涂了。不能肯定地说,但是如果你再次使用 c_str(),你应该可以开始了。

这里的问题是 std::basic_ofstream::open 在 C++03 中没有接受 std::string、 的重载。但是,在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");