Solved

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

Posted on 2002-06-29
2
228 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

734 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