SFML community forums

Help => System => Topic started by: cppvoid on June 12, 2016, 01:15:41 pm

Title: Save/Load a vector<sf::String> without data lost
Post by: cppvoid on June 12, 2016, 01:15:41 pm
What is the simplest method to save or load a couple of sf::Strings without data lost?

   std::ifstream file("example.txt");
   string standardLine;

   if(!file)    return false;

   while(std::getline(file, standardLine))
   {
         std::basic_string<sf::Uint32> utf32;
         sf::Utf8::toUtf32(standardLine.begin(), standardLine.end(), std::back_inserter(utf32));
         
         strings.push_back(utf32);
   }
 

This seems not to work correctly the output looks like the wrong encoding. example.txt is filled with Unicode characters and the output of a loaded sf::String looks like this: [][][][][][][]
Title: Re: Save/Load a vector<sf::String> without data lost
Post by: Mr_Blame on June 12, 2016, 04:24:52 pm
Do you output through std::cout? Don't do it because std::cout works woth UTF-8 encoding.
Title: Re: Save/Load a vector<sf::String> without data lost
Post by: eXpl0it3r on June 12, 2016, 06:01:17 pm
Any reasons you take the detour via std::basic_string?
Why don't you just call getData() on the sf::String, write it in binary mode to a file and then read in binary mode from it again and pass the read data to the sf::String constructor?
Title: Re: Save/Load a vector<sf::String> without data lost
Post by: Laurent on June 12, 2016, 06:36:41 pm
If you really need to use a human-readable representation of your text, just use String::fromUtf8 and String::toUtf8. Unless you wrote the text file manually and saved it with the wrong encoding, it should just work fine.