You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Printing to stringstream and retaining formatting #116
Could you redirect std::cout to a buffer? I'm not sure if this works but it's worth a try.
std::stringstream buffer;
std::streambuf* prevcoutbuf = std::cout.rdbuf(buffer.rdbuf());
// print your table
std::cout << yourTable << std::endl;
// Get the table as a string
std::string text = buffer.str();
// Restore original buffer before exiting
std::cout.rdbuf(prevcoutbuf);
Could you redirect std::cout to a buffer? I'm not sure if this works but it's worth a try.
std::stringstream buffer;
std::streambuf* prevcoutbuf = std::cout.rdbuf(buffer.rdbuf());
// print your table
std::cout << yourTable << std::endl;
// Get the table as a string
std::string text = buffer.str();
// Restore original buffer before exiting
std::cout.rdbuf(prevcoutbuf);
So let me summarize it. If you have table the standard way of printing is:
std::cout << table;
When you want to capture the string, you can use table.str().c_str()
However, when you print it, the formatting (notably colors) is lost:
printf("%s", table.str().c_str()); // formatting and colors are lost
The workaround that @p-ranav provided is a good one and works as expected. You can capture the string and do whatever you want with it. Also print it with printf() with all the formatting retained:
std::stringstream buffer;
std::streambuf* prevcoutbuf = std::cout.rdbuf(buffer.rdbuf());
std::cout << table << std::endl;
std::string text = buffer.str();
printf("%s", text.c_str()); // formatting and colors are good
std::cout.rdbuf(prevcoutbuf);
It would be great if Tabulate had a function for capturing the string, but this workaround works just fine.
Quick follow-up to my comment above: the rdbuf swap does not help if the table contains a nested table. That one loses its colors for a different reason.
Activity
p-ranav commentedon Apr 2, 2024
Could you redirect std::cout to a buffer? I'm not sure if this works but it's worth a try.
suitmyself commentedon Oct 15, 2024
not work
nepto commentedon Feb 1, 2025
It actually does work.
So let me summarize it. If you have
tablethe standard way of printing is:When you want to capture the string, you can use
table.str().c_str()However, when you print it, the formatting (notably colors) is lost:
The workaround that @p-ranav provided is a good one and works as expected. You can capture the string and do whatever you want with it. Also print it with
printf()with all the formatting retained:It would be great if Tabulate had a function for capturing the string, but this workaround works just fine.
nepto commentedon Sep 29, 2026
Quick follow-up to my comment above: the
rdbufswap does not help if the table contains a nested table. That one loses its colors for a different reason.Fixed in #137