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

Author Topic: Save/Load a vector<sf::String> without data lost  (Read 3878 times)

0 Members and 1 Guest are viewing this topic.

cppvoid

  • Newbie
  • *
  • Posts: 1
    • View Profile
    • Email
Save/Load a vector<sf::String> without data lost
« 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: [][][][][][][]

Mr_Blame

  • Full Member
  • ***
  • Posts: 192
    • View Profile
    • Email
Re: Save/Load a vector<sf::String> without data lost
« Reply #1 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.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: Save/Load a vector<sf::String> without data lost
« Reply #2 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?
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Save/Load a vector<sf::String> without data lost
« Reply #3 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.
Laurent Gomila - SFML developer

 

anything