Not sure what's wrong, maybe you are getting repeated content.
Change that:
std::ofstream out(file, std::ios::binary | std::ios_base::app | std::ios_base::out);
to that:
std::ofstream out(file, std::ios::binary);
and you should be fine, if your string is fine.
Check this minor example:
#include <iostream>
#include <fstream>
#include <string>
#include <iterator>
#include <vector>
using namespace std;
int main() {
string m_strReceivedMessage = "foo";
const char * c = m_strReceivedMessage.c_str();
ofstream out("test.bin", ios::binary);
int i = m_strReceivedMessage.size();
if (out.is_open()) {
out.write(c, i);
}
out.close();
ifstream input("test.bin", ios::binary );
// copies all data into buffer
vector<char> buffer((
istreambuf_iterator<char>(input)),
(istreambuf_iterator<char>()));
for(unsigned int i = 0; i < buffer.size(); ++i)
cout << buffer[i] << endl;
return 0;
}
which outputs:
gsamaras@gsamaras-A15:~$ g++ -Wall px.cpp
gsamaras@gsamaras-A15:~$ ./a.out
f
o
o
For more, please read Reading and writing binary file.