std::basic_ofstream<char, std::char_traits<char> >::open(std::string&)編譯此代碼時出現錯誤:
FileEXt = ".conf";
const char* FileEX = FileEXt.c_str();
const char* File = Uname FileEX;
string File = Uname FileEXt;
ofstream outFile;
outFile.open(File);
完整代碼:
LPCSTR lpPathName = ".\\DB";
SetCurrentDirectoryA(lpPathName);
string Uname, Pword;
cout << "Please enter a name: ";
cin >> Uname;
cout << '\n' << '\n';
cin >> Pword;
system("CLS");
cout << "Username: " << Uname << '\n' << "Password: " << Pword << '\n';
const char* FileEXt = ".conf";
const char* Unames = Uname.c_str();
const char* FileEX = FileEXt;
string File = Uname FileEXt;
ofstream outFile;
outFile.open(File);
if ( outFile.fail() )
{
outFile << Uname << '\n' << Pword;
outFile.close();
}
else
{
cout << Uname << " already exists!" << '\n';
Sleep(3000);
return 0;
}
此代碼應該創建一個在 DB 目錄中存盤名稱的檔案。
uj5u.com熱心網友回復:
你正在傳遞一個std::stringto ofstream::open()。在 C 11 之前,open()不接受 astd::string作為輸入,只接受一個const char*指標,例如:
outFile.open(File.c_str());
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/456484.html
