Simple question: why the following code isn’t working? I would expect a string to be saved and read, but this just doesn’t happen…
#include <iostream>
#include <fstream>
using namespace std;
static string path = "/Users/john/Desktop/file";
main() {
string a;
a = "one\n";
fstream outStream(path.c_str(), ios::out | ios::binary);
outStream.write((char *) &a, sizeof(a));
outStream.close();
a = "two\n";
fstream inStream(path.c_str(), ios::in | ios::binary);
inStream.read((char *) &a, sizeof(a));
inStream.close();
cout << a;
return 0;}
Thanks a lot!
You cant write a string as a pointer. Either use the normal stream operators (
<<) or use