[英]How to solve "std::basic_ofstream<char, std::char_traits<char> >::open(std::string&)"
I get an error about std::basic_ofstream<char, std::char_traits<char> >::open(std::string&)
when compiling this code:编译此代码时,我收到有关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);
Full code:完整代码:
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;
}
This code is supposed to create a file that stores a name in the DB directory.此代码应该创建一个文件,将名称存储在 DB 目录中。
You are passing a std::string
to ofstream::open()
.您正在将std::string
传递给ofstream::open()
。 Prior to C++11, open()
did not accept a std::string
as input, only a const char*
pointer, eg:在 C++11 之前, open()
不接受std::string
作为输入,只接受const char*
指针,例如:
outFile.open(File.c_str());
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.