I would like to refer (write) to a std::string like I would to a string literal (char*). This is what I'm trying to do:
std::string File::readAll(){
std::string ret;
ret.reserve(this->size+1);
fseek(file, 0, SEEK_SET);
fread((char*)ret.c_str(), size, 1, this->file);
ret.append("\0");
return ret;
}
The string remains empty when I do this. How can I make this work?
Replace reserve
with resize
and then use .data()
(or &ret[0]
pre C++17) instead of .c_str()
.