Click here to Skip to main content
15,892,697 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am writing a code in which I need to create a std::string by appending data to it(here data means logs of an application). But string also has its limitation of size. I have written the code in try-catch block and when append() fails, it throws exception. I need to check before append() whether attaching new string will cause any exception, since once exception is thrown, it clears the data from string.
I want to check if attaching new string will cause any exception and if yes then write the data to a file and clear the string for next input.
As it is an application log, I do not know what the final size will be. So I am trying to make code less vulnerable to above explained problem.
I have checked one condition as follows:
If "current_string_size + to_be_attached_string_size exceeds string.max_size()" then write the code into file.
If "not" then append to_be_attached_string to current_string.
But it is failing somewhere.
What condition I need to check?
Posted

1 solution

Quote:
If "current_string_size + to_be_attached_string_size exceeds string.max_size()" then write the code into file.
If "not" then append to_be_attached_string to current_string.
But it is failing somewhere.

So, are you getting a bad_alloc exception?
I wonder why you don't write the string more frequently to the file.
 
Share this answer
 
Comments
Sam L 23-Sep-15 6:15am    
@CPallini: Yes, I am getting bad_alloc exception.
I need to minimize file I/O as possible.
CPallini 23-Sep-15 6:19am    
But then, in addition to being prone to the bad_alloc exception, on application crashes you would get a rather outdated log.
Sam L 23-Sep-15 6:33am    
yes. Then I would prefer to write into the file when string size reaches some user-defined size.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900