I would use this usually (like when we're not reading thousands of files in a loop!):
std::ifstream file("data.txt");
std::string data {std::istreambuf_iterator<char>{file},
std::istreambuf_iterator<char>{} };
No need to use std::copy
(like the other answer; now deleted!).
If you want vector, then use this instead:
std::vector<char> data {std::istreambuf_iterator<char>{file},
std::istreambuf_iterator<char>{} };
However, if you want to populate an existing std::vector
(or std::string
), then use insert
method (both types has insert
method of same signature!):
data.insert(data.end(), //insert at end
std::istreambuf_iterator<char>{file}, //source begin
std::istreambuf_iterator<char>{} ); //source end
Hope that helps.