The file is not empty, but ifstream
returns an empty string.
Here is what I have tried:
std::ifstream ifs;
ifs.open("joke");
ifs >> ymjoke.c_str();
ShowMessage(ymjoke);
Here is a screenshot of the ShowMessage()
window:
I'm assuming that ymjoke
is declared as an AnsiString
, yes? Why are you reading into its c_str()
pointer? Did you pre-allocate ymjoke
's memory beforehand? I'm betting you did not. You are using the overloaded operator>>
that reads into a char[]
buffer, so it expects that buffer to be large enough to hold the text being read into it, eg:
AnsiString ymjoke;
ymjoke.SetLength(256); // or whatever size suits your need
std::ifstream ifs("joke");
ifs >> std::setw(ymjoke.Length()+1) >> ymjoke.c_str();
ShowMessage(ymjoke.c_str());
That said, the VCL has an overloaded operator>>
for reading an AnsiString
from a std::istream
. You just need to enable it manually, by adding VCL_IOSTREAM
to the Conditionals list in your project options. Then, you can use it like this:
AnsiString ymjoke;
std::ifstream ifs("joke");
ifs >> ymjoke;
ShowMessage(ymjoke);
Otherwise, read into a std::string
instead, eg:
std::string ymjoke;
std::ifstream ifs("joke");
ifs >> ymjoke;
ShowMessage(ymjoke.c_str());