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

Author Topic: [solved] sf::Image::LoadFromFile get error text  (Read 4230 times)

0 Members and 1 Guest are viewing this topic.

chocobn

  • Newbie
  • *
  • Posts: 39
    • View Profile
[solved] sf::Image::LoadFromFile get error text
« on: April 18, 2011, 08:50:07 pm »
Hello,
When loading some images, sometimes it could not work (wrong file format, image too big, or anything else...). When it doesn't work, is there a way to get the error as a string, in order to display it in a message box?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
[solved] sf::Image::LoadFromFile get error text
« Reply #1 on: April 18, 2011, 09:12:30 pm »
You can redirect std::cerr to a string stream.

Code: [Select]
#include <sstream>

std::ostringstream output;
std::cerr.rdbuf(output.rdbuf());

if (!image.LoadFromFile("..."))
{
    std::string error = output.str();
}


I admit this is more a hack than a clean solution, I'll probably think about redesigning the error handling system in SFML.
Laurent Gomila - SFML developer

chocobn

  • Newbie
  • *
  • Posts: 39
    • View Profile
[solved] sf::Image::LoadFromFile get error text
« Reply #2 on: April 18, 2011, 09:16:46 pm »
Ok thanks! It would indeed be really be useful to have something like sf::Image::GetError or something like that, but while I have something that works, i'm happy  :P thanks for the trick that's useful (and as far as i have seen, it seems to be handy in lots of other contexts)

chocobn

  • Newbie
  • *
  • Posts: 39
    • View Profile
[solved] sf::Image::LoadFromFile get error text
« Reply #3 on: April 18, 2011, 09:37:43 pm »
Just a little modification for people that may look for an answer later : std::cerr was not the correct stream. Here is the code with the good one for SFML errors :
Code: [Select]

#include <SFML/System.hpp>
[...]
std::ostringstream output;
sf::Err().rdbuf(output.rdbuf());

    if (!image->LoadFromFile("..."))
    {
std::string error = output.str();
std::cout<< "erreur!"<<error <<std::endl;
    }


Thanks a lot for the help.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
[solved] sf::Image::LoadFromFile get error text
« Reply #4 on: April 18, 2011, 09:45:43 pm »
You didn't mention that you are using SFML 2 ;)

std::cerr is correct for SFML 1.6.
Laurent Gomila - SFML developer