I've been trying to make my program write a string into a binary file using Ofstream::write(), but I could not find out how to (through the interwebs), so I tried writing a struct with a string into the file. That worked perfectly; I could open the file and read the string (with my human eyes), but when I tried to use Ifstream::read() to read the struct, I just got an empty string and the string that I wrote (in this case, "dir" was the empty one, and "fileName" was correctly read). Any and all help is appreciated :) PS: Both strings are saved in the file... This is my writing code:
StringStruct texPath;
texPath.dir = "src/Assets/";
texPath.fileName = "bricks_top.png";
file.write((char*)&texPath, sizeof(texPath));
This is my reading code:
StringStruct texFile;
file.read((char*)&texFile, sizeof(texFile));
std::string filepath = "";
filepath += texFile.dir;
filepath += texFile.fileName;
std::cout << filepath;
And this is the "StringStruct" code:
struct StringStruct {
std::string dir = "src/Assets/";
std::string fileName = "Example.png";
};
Ok, I recieved some comments (thanks manni66) saying that I have to write as c-strings. So I changed my struct to this:
struct StringStruct {
char* dir = "src/Assets/";
char* fileName = "Example.png";
};
So that I was writing each string as a c-string instead.