-1

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?

Serket
  • 1,865
  • 1
  • 5
  • 23

1 Answers1

4

Replace reserve with resize and then use .data() (or &ret[0] pre C++17) instead of .c_str().

Ayxan Haqverdili
  • 17,764
  • 5
  • 27
  • 57