Re: Returning Char array/pointer? Continuing of thread I am confused
with these concepts.
This is my function using std::string
std::string ReadFile2String(char const* aSrcFile){
ifstream in;
std::string strRtn;
long lngFileSize = 0;
in.open(aSrcFile, ios::in| ios::binary | ios::ate);
if(in.is_open()||in.bad())
{
lngFileSize = in.tellg();
}else{
throw exception("could not open input file"); // could not open in
file
return NULL;
}
char* chrTmp = new char[lngFileSize];
in.seekg(0,ios::beg);
in.read(chrTmp, lngFileSize);
strRtn = chrTmp; ???????
return strRtn;
}
use the function overrides that accept a length. what is the function?
My function does not return the correct result, the strRtn contains
extra bytes like AB AB AB FE FE FE....
On Feb 11, 3:39 pm, "Ben Voigt [C++ MVP]" <r...@nospam.nospam> wrote:
I am working on
std:string ReadFileToString( const char* fileName)
My concern is my file is an encrypted file and I open it as binary,
what if it happen contains '\0' in the middle, did the string get
truncated.
std::string stores the length separately instead of depending on a
terminator, to store embedded nulls you just have to use the function
overrides that accept a length.
However std::vector<char> is probably better in your case since you are
wanting to treat the data more like an array than a string. std::vector=
you
can directly use as the buffer for file I/O, with std::string you are
expected to pass in a buffer already containing the data (either to the
constructor or to the append member function).
Again, thanks- Hide quoted text -
- Show quoted text -