I am writing code to write a vector
to file. My aim is to write the last half of the vector
to file first and then the first half based on offset. The code below gives me segmentation fault.
std::vector<uint8_t> buffer_(1000); // the vector is filled with values
int offset_ = 300;
std::ofstream output_file (file_name.c_str(), std::ofstream::out | std::ofstream::binary);
if (output_file.is_open()) {
output_file.write(reinterpret_cast<const char*>(&buffer_[offset_]), (buffer_.size() -offset_)*sizeof(uint8_t));
// segmentation fault on the line above
output_file.write(reinterpret_cast<const char*>(&buffer_[0]), (offset_)*sizeof(uint8_t));
}
Can somebody tell me whats wrong with the code?