0

For the following code, is it possible to output the result to a string instead of wofstream? Thanks you!

wstring w = L"test";
std::wofstream ofs("test.txt");
std::locale utf8_locale(std::locale(), new boost::archive::detail::utf8_codecvt_facet());
ofs.imbue(utf8_locale);
std::copy(w.begin(),w.end(),
    std::ostream_iterator<wchar_t, wchar_t>(ofs));
ST3
  • 8,826
  • 3
  • 68
  • 92
echo
  • 789
  • 2
  • 12
  • 21
  • http://en.cppreference.com/w/cpp/io/basic_stringstream – chris Sep 06 '12 at 02:31
  • Hi Chris, could you elaborate it further with some code? I am not familiar with C++ – echo Sep 06 '12 at 02:35
  • 2
    `std::wostringstream` is derived from the same class as `std::wofstream`, so replacing just the type of variable used should still be compatible with the other functions you use. Stringstreams maintain a string rather than a file, though (accessed via `str()`), which should fulfill your needs. – chris Sep 06 '12 at 02:39
  • Like std::string output = woss.str();? Sorry for the dumb question again, how do you know wostringstream and wofstream are derived from a same class? – echo Sep 06 '12 at 02:57
  • I understand now. This is also helpful: http://en.cppreference.com/w/cpp/io – echo Sep 06 '12 at 03:08
  • Very. If you wanted a direct reference, the standard would be the place to look. It's a bit easier to explain with pictures, though :) Just because I can, I found this in § 27.3/5: `The class template specialization basic_ostream serves as a base class for class templates basic_ostringstream and basic_ofstream.` – chris Sep 06 '12 at 03:10

1 Answers1

2

Output string streams are a C++ feature that behave similarly to output file streams in the sense that they both inherit from std::ostream, meaning that you can use mostly the same functions in both. However, string streams operate on a string rather than a file.

What this means for you is that all you need to do (as you haven't used any wofstream-specific functions) is change the type of ofs to a string stream:

#include <sstream>
std::wostringstream oss;

Now I couldn't get the locale stuff building (I haven't used those as of yet), but commenting them did indeed produce correct results (see this test). As you can see, you can access the string via the string stream's str() function.

chris
  • 60,560
  • 13
  • 143
  • 205
  • Right. But actually str() returns a wstring, not a string... Thanks for your answer! – echo Sep 06 '12 at 03:02
  • @echo, With a bit of snooping, you'll find that both `std::ostringstream` and `std::wostringstream` are actually the same basic class, but with a different template argument. The former's is `char` and the latter's is `wchar_t`. The `str()` function returns a `std::basic_string`, where `CharT` is the template argument. This basically evaluates to a `std::string` or `std::wstring` respectively, with the exact same code. If you wanted the whole mess to use your own char type, you could say `std::basic_ostringstream` to have no code changes produce correct types. – chris Sep 06 '12 at 03:06