Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9117345
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T04:52:39+00:00 2026-06-17T04:52:39+00:00

I have some existing code that uses a std::ostringstream as an temporary buffer. To

  • 0

I have some existing code that uses a std::ostringstream as an temporary buffer. To increase robustness, I want to define an upper limit for the buffer size (e.g., 16 KB). When the limit is exceeded, all following data that is append, should be silently discarded. Ideally, after logging a warning.

What is the simplest way of doing it? std::ofstream seems to have no efficient way of getting its current size. I can only think of my_stream.str().size(), which seems to be highly inefficient.

Of course, I can manually keep track of the number of inserted chars by keeping an extra counter, but maybe there is an elegant alternative that I am missing. I saw that ofstringstream has an internal buffer (rdbuf()) that can be replaced.

Is it possible (and practical) to use it to solve my problem?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T04:52:40+00:00Added an answer on June 17, 2026 at 4:52 am

    One possible solution is to extend stringbuf into your own class and use that within ofstringstream. Then it should be trivial to implement your max limit. It looks like stringbuf::overflow is the function you want to overload.

    Alternatively you could wrap the std::ostringstream within another class which checks size before writing to it. How do you conclude that this is inefficient? As long as the size is not calculated every call, it is just the matter of navigating down the indirection.

    Edit: Missed the word “copy” in the reference, but yes, once you wrap it, you could keep your own counter.

    Alternatively, you could use tellp to try and figure out how many characters are written, as long as it always points to the end.

    Return Value
    An integral value of type streampos with the number of
    characters between the beginning of the output sequence and the
    current position.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some existing code that uses an enum instead of a function pointer
I have some existing C code that uses ICMP raw sockets to do Ping
I have some existing code that worked fine under Windows 2003, to obtain the
I'm adding some functionality to an existing code base that uses pure C functions
I'm new to StructureMap and have some existing code that I'm working with that
I have some existing code. This code uses an html table for a banner.
I have some existing code which isn't formatted consistently -- sometimes two spaces are
I have some existing custom Excel workbooks/applications with code-behind C#, which work fine. They
I have problem to add jQuery to some existing code, please help. We have
I'm looking at existing python code that heavily uses Paramiko to do SSH and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.