Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C++ and Visual C++ > C++ Programming
|
C++ Programming General discussions for the C++ language. For questions specific to Microsoft's Visual C++ variant, see the Visual C++ forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C++ Programming section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old March 14th, 2015, 12:37 PM
Registered User
 
Join Date: Mar 2015
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default C: Dynamic string using variable number of arguments

I need to create dynamic string by given format(%d,%s,%f,%lf,%c) using variable number of arguments in function. This code gives me an error(main.exe has stopped working):

Code:
#include<stdio.h>
#include<stdarg.h>
#include<string.h>
#include<stdlib.h>

char *form(char *format,...);
char *form(char *format,...)
{
    char *res=(char *)calloc(1,1),val_int[12],*pos_int,*val_str,*pos_str,*val_float,*pos_float,*val_lf,*pos_lf,*val_char,*pos_char;
    va_list args;
    va_start(args,format);
    do
    {
      pos_int=strstr(format,"%d");pos_str=strstr(format,"%s");pos_float=strstr(format,"%f");pos_lf=strstr(format,"%lf");pos_char=strstr(format,"%c");
      if(pos_int && (!pos_str || pos_int < pos_str) && (!pos_float || pos_int < pos_float) &&
         (!pos_lf || pos_int < pos_lf) && (!pos_char || pos_int < pos_char))
      {
        itoa(va_arg(args,int),val_int,10);
        res=(char *)realloc(res,strlen(res)+(pos_int-format)+strlen(val_int)+1);
        strncat(res,format,pos_int-format);
        strcat(res,val_int);
        format=pos_int+2;
      }
      else if(pos_str && (!pos_int || pos_str < pos_int) && (!pos_float || pos_str < pos_float) &&
              (!pos_lf || pos_str < pos_lf) && (!pos_char || pos_str < pos_char))
      {
         val_str=va_arg(args,char *);
         res=(char *)realloc(res,strlen(res)+(pos_str-format)+strlen(val_str)+1);
         strncat(res,format,pos_str-format);
         strcat(res,val_str);
         format=pos_str+2;
      }

      else if(pos_float && (!pos_int || pos_float < pos_int) && (!pos_str || pos_float < pos_str) &&
              (!pos_lf || pos_float < pos_lf) && (!pos_char || pos_float < pos_char))
      {
         fcvt(va_arg(args,double),val_float,6,0);
         res=(char *)realloc(res,strlen(res)+(pos_float-format)+strlen(val_float)+1);
         strncat(res,format,pos_float-format);
         strcat(res,val_float);
         format=pos_float+2;
      }

      else if(pos_lf && (!pos_int || pos_lf < pos_int) && (!pos_str || pos_lf < pos_str) &&
              (!pos_float || pos_lf < pos_float) && (!pos_char || pos_lf < pos_char))
      {
          ecvt(va_arg(args,double),val_lf,6,0);
          res=(char *)realloc(res,strlen(res)+(pos_lf-format)+strlen(val_lf)+1);
          strncat(res,format,pos_lf-format);
          strcat(res,val_lf);
          format=pos_lf+2;
      }
      else if(pos_char && (!pos_int || pos_char < pos_int) && (!pos_str || pos_char < pos_str) &&
              (!pos_float || pos_char < pos_float) && (!pos_lf || pos_char < pos_lf))
      {
          val_char=va_arg(args,char *);
          res=(char *)realloc(res,strlen(res)+(pos_char-format)+strlen(val_char)+1);
          strncat(res,format,pos_char-format);
          strcat(res,val_char);
          format=pos_char+2;
      }
    }
    while(pos_int || pos_str || pos_float || pos_lf || pos_char);
    va_end(args);
    res=(char *)realloc(res,strlen(res)+strlen(format)+1);
    strcat(res,format);
    return res;
}

int main()
{
    char *s;
    s=form("John is %d years old and has an %c in %s. He is going to school for %lf years.",9,'A',"maths",1.5);
    printf("%s",s);
    free(s);
    return 0;
}
I assume the error is in functions(itoa,fcvt,ecvt). Thanks for replies.
Reply With Quote





Similar Threads
Thread Thread Starter Forum Replies Last Post
Test if variable is even or odd number Mikect Beginning VB 6 2 June 13th, 2016 08:17 AM
Load VARIABLE Number of columns cosmokramer Oracle 1 November 2nd, 2008 05:51 PM
how to replace a string with another string/number crmpicco Javascript How-To 4 March 14th, 2005 12:59 PM
variable number of fields in record manisha.anand Oracle 0 July 1st, 2004 05:00 PM
Query String Arguments kevind23 Classic ASP Basics 4 June 29th, 2004 10:16 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.