Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2002-06-29
2
Medium Priority
?
235 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 400 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: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Article by: evilrix
Looking for a way to avoid searching through large data sets for data that doesn't exist? A Bloom Filter might be what you need. This data structure is a probabilistic filter that allows you to avoid unnecessary searches when you know the data defin…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
Suggested Courses

876 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