Solved

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

Posted on 2002-06-29
2
222 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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

708 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now