Solved

The result buffer must be large enough to hold the result string

Posted on 2002-06-29
2
227 Views
Last Modified: 2010-08-05
This program is generating error since I am not able to set/increase the buffer size of the resulting string.
For complete refernce to this problem plz refer to the comments and my previous question
"06/27/02 100 Replace single doublequotes with double doublequot... "

The result buffer must be large enough to hold the result string. The result
string will be at least at strlen(src) + 2 (for the start and end double
quotes), and can be larger if there are double quotes which must be converted into 2
double quote escapes.

char *
DoubleQuotedStringWrite(char* result, char* src)
{
  char* dest = result;
  *dest++ = '\"'; // write the starting double quote
/* strcpy() the src into the dest, expanding any double  
   quotes into 2 double quote escape sequences.*/
   
    for(char* psrc = src ; *psrc ; ++psrc) {
       *dest++ = *psrc;
       if(*psrc == '"') {
         // write another " to make it a 2 double quote
          escape sequence
        *dest++ = *psrc;
       }
    }
    *dest = '\"'; // write the ending double quote
    return(result);
}
0
Comment
Question by:manbirpahwa
2 Comments
 
LVL 32

Expert Comment

by:jhance
ID: 7118256
>>since I am not able to set/increase the buffer size of
>>the resulting string.

WHY ARE YOU NOT ABLE TO INCREASE THE SIZE OF THE BUFFER?

Do you NOT know how? (We can help here.)

Do you have broken fingers and can't type the code? (Can't help much...  See a doctor.)

What?
0
 
LVL 1

Accepted Solution

by:
Levant earned 100 total points
ID: 7119604
Use the following function to calculate the resulting buffer size:

int BufferSize(char *src)
{
   int num = 0;   // holds number of double quotes

   for(char *ptr = src; *ptr; ptr++)
   {
      if(*ptr == '\"')
         num++;
   }

   return(strlen(src) + num + 1);
}

Then in order to execute the DoubleQuotedStringWrite correctly use:

int size = BufferSize(str);
char *result = (char *)malloc(size);
DoubleQuotedStringWrite(result, str);

Now result contains just enough space.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question