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:
> ymjoke.c_str(); Sh" /> > ymjoke.c_str(); Sh" /> > ymjoke.c_str(); Sh"/>
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
ymjokeis declared as anAnsiString, yes? Why are you reading into itsc_str()pointer? Did you pre-allocateymjoke's memory beforehand? I'm betting you did not. You are using the overloadedoperator>>that reads into achar[]buffer, so it expects that buffer to be large enough to hold the text being read into it, eg:That said, the VCL has an overloaded
operator>>for reading anAnsiStringfrom astd::istream. You just need to enable it manually, by addingVCL_IOSTREAMto the Conditionals list in your project options. Then, you can use it like this:Otherwise, read into a
std::stringinstead, eg: