With c++17, the pointer returned by .data() is now non-const so the memory referenced by .data() can be altered. The string constructor creates a string of at least the required size so the .read() will happily read the contents of the file into the string memory. Note this is c++17. Previous c++ standards have .data() returning a const pointer so the string memory can't (shouldn't) be altered like this.