Hello, I am trying to append a sf::string by adding text of type sf::Text. Then i want to write this file with a ofstream to a textfile. What i need this for is to save the history from the text input in my game.
Example Code):
sf::String string1 = "";
sf::Text text1("Input: ", somefont, somesize);
sf::Text text2("Go north ", somefont, somesize);
sf::Text text3("Going north!", somefont, somesize);
string1 += text1.getString();
string1 += text2.getString();
string1 += text3.getString();
std::ofstream myfile;
myfile.open ("History.txt");
myfile << string1; //Error here, cant add a sf::string
myfile.close();
I have tried casting the sf::string after the appending to a std::string like this: std::string temp = string1;
but that doesent work either. whats the correct way of doing a appending and filewriting with a sf::string?
Would love to see some code examples aswell since im quite new to Sfml!
but that doesent work either.
Can you give a meaningful error description?
If you don't have specific characters that can't be ASCII-encoded, converting to std::string is indeed the correct solution in order to write to a std::ofstream. Keep also in mind that std::ofstream only works with char, for other character types you need a different std::basic_ofstream instantiation.
By the way, have a look at RAII. The code
std::ofstream myfile;
myfile.open ("History.txt");
...
myfile.close();
is unnecessarily complex, simply use
std::ofstream myfile("History.txt");
...