Solved

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

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

Industry Leaders: 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 a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
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 learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

635 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