C++ C++;流可以';t从变量名打开文件(即使在使用c_str()之后也不打开)

C++ C++;流可以';t从变量名打开文件(即使在使用c_str()之后也不打开),c++,c++11,filestream,C++,C++11,Filestream,在我的代码中,我试图创建一个文件并写入其中: std::ofstream saveFile("file.txt"); ofstream << "test" << endl; std::ofstream saveFile(“file.txt”); 它应该能起作用。但是你真的发布了正确的代码吗 至少您的代码有几个明显的语法错误: std::string fileName = "file.txt"; // no semicolon std::ofstream saveFile

在我的代码中,我试图创建一个文件并写入其中:

std::ofstream saveFile("file.txt");
ofstream << "test" << endl;
std::ofstream saveFile(“file.txt”);
它应该能起作用。但是你真的发布了正确的代码吗

至少您的代码有几个明显的语法错误:

std::string fileName = "file.txt"; // no semicolon
std::ofstream saveFile(fileName.c_str()); // 'fileName' rather than 'filename'
saveFile << "test" << endl; // don't redirect "test" to std::ofstream
// std::ofstream is a class rather than an instance
std::string fileName=“file.txt”;//没有分号
std::ofstream saveFile(fileName.c_str());/'“文件名”而不是“文件名”

谢谢你的回复。原来问题是在文件名的字符串中传递了无效字符。顺便说一句:如果你解决了自己的问题,那就是自我回答的意义所在。别把他们扯进来!
std::string fileName = "file.txt"; // no semicolon
std::ofstream saveFile(fileName.c_str()); // 'fileName' rather than 'filename'
saveFile << "test" << endl; // don't redirect "test" to std::ofstream
// std::ofstream is a class rather than an instance