为什么c_str在这种打开(文件名)的情况下不起作用

Why is c_str not working in this open(filename) case

本文关键字:文件名 不起作用 情况下 str 为什么      更新时间:2023-10-16

我以为我理解在打开文件时必须将std::字符串转换为*char,但我缺少了一些东西。它编译良好,但不会打开。尝试了许多变体,但到目前为止,只对文件中的名称进行硬编码:

//  const char * cEMN = cCCA.get_EMNfn().c_str();
//  femn.open(cEMN);  fails
//  femn.open("file-foo.emn"); works
string stdEMN;
stdEMN = cCCA.get_EMNfn();
femn.open(stdEMN.c_str());  // fails
if(!femn)
{
    cout << "Open of Original EMN file failedn";
    cout << "EMN file: " << cCCA.get_EMNfn() << endl;
    cout << "Press any key to exit" << endl;
    ch = getchar();
    return 1;
}

据我所知,事实是:

femn.open("file-foo.emn");

成功。但是

femn.open(stdEMN.c_str());

失败。

显而易见的结论是stdEMN.c_str()的计算结果是一个不同于"file-foo.emn"的字符串。