Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Appending and writing a sf::string to a file.  (Read 3404 times)

0 Members and 2 Guests are viewing this topic.

Bobson

  • Newbie
  • *
  • Posts: 3
    • View Profile
Appending and writing a sf::string to a file.
« on: March 26, 2014, 03:41:08 pm »
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!

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Appending and writing a sf::string to a file.
« Reply #1 on: March 26, 2014, 03:52:25 pm »
Quote
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");
...
« Last Edit: March 26, 2014, 04:10:18 pm by Nexus »
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Bobson

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: Appending and writing a sf::string to a file.
« Reply #2 on: March 26, 2014, 04:12:49 pm »
Works as intended now, The issue didnt have anything to do with sfml , I didnt append the string properly in the function i used and it didnt write anything to my file.

 

 

anything