-1

I thought I understood having to cast a std::string as a *char when opening a file, but I am missing something. It compiles fine but does not open. Tried a number of variations but so far only hardcoding the name in the file is working:

//  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 failed\n";
    cout << "EMN file: " << cCCA.get_EMNfn() << endl;
    cout << "Press any key to exit" << endl;
    ch = getchar();
    return 1;
}
tshepang
  • 12,111
  • 21
  • 91
  • 136

1 Answers1

3

The facts as I discern them to be are that:

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

succeeds. But

femn.open(stdEMN.c_str());

fails.

The obvious conclusion is that stdEMN.c_str() evaluates to a string that differs from "file-foo.emn".

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
  • Not the case as you can see my error code sends the filename to the screen in the case of a failure and unless I'm going blind they appear to be the same thing – user3037031 Nov 26 '13 at 15:43
  • If stdEMN.c_str() does evaluates equal to "file-foo.emn", then the call to `open` will succeed. Surely. Unless the computer hates you. Not likely. – David Heffernan Nov 26 '13 at 15:48
  • I'd look at the bytes and make sure there isn't extra unprintable stuff in there (Or different encodings?) because this is almost certainly what's happening. – Caribou Nov 26 '13 at 15:48