Stringstream sgetn returns NULL on iOS 5.1

I have some code that copies the content std::stringstream

tochar * dest

    static size_t copyStreamData(std::stringstream & ss, char * const & source, char * dest)
        ss << source;
        size_t ret = ss.rdbuf()->sgetn( dest, (std::numeric_limits<size_t>::max)() ) ;
        dest[ret] = 0;
        return ret;


on iOS 5.0 and below it works fine as expected ... But on iOS 5.1 it returns NULL.

What am I doing wrong? See also How can I fix my code?


source to share

2 answers

You must choose a string representation.

If you need to use C strings, for some reason your function is called strncpy


std::strncpy(dest, source, max_size_of_dest);


Check out the caveats in the link.

If you can use a better abstraction, you are encouraged to upgrade to std::string


void copy(std::string const& source, std::string& dest) { dest = source; }


No need to deal with the length of the buffer (and thus no messing around more often), this is a very powerful help.

Note that nothing prevents you from manipulating std::string

within your application and still communicating with C methods: it .c_str()

helps a lot.



What you are trying to do is basically the same as doing:

std::size_t length = std::strlen(source) + 1; // + 1 for '\0'
std::copy(source, source + length, dest);
// Assuming dest has length + 1 bytes allocated for it


I doubt it sgetn

returns NULL as it sgetn

returns std::streamsize

and not a pointer type. Does it return 0 or does another function return NULL? Have you tried to clear the stream before calling rdbuf()




All Articles