C string creation from character format string? (very basic)

Hi Experts,

When using the standard library, I usually create string by using string directly or with ostringstream (<< operators)....

I like printf an awful lot though, and would love to be able to just do this if I wanted:

string s = myFormatFunction("%c%c%c%c%c", 'h', 'e', 'l', 'l', 'o');

after the call, of course, we get, "hello" in s.

There must already be a flavor of printf that does this or something similar in the standard library (c or C++)?  I can't seem to find it....

Thanks,
Mike
LVL 1
threadyAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
ZoppoConnect With a Mentor Commented:
Hi thready,

I'm not sure if this works for you since I guess the functions _vscprintf and vsprintf maybe are compiler specific, at least it works with VisualStudio.'s C++ compiler - if you use some other please tell.
std::string myFormatFunction( const char* pszFormat, ... )
{
	va_list args;

	va_start( args, pszFormat );

	int nLen = _vscprintf( pszFormat, args );

	char* pszBuffer = new char[ nLen + 1 ];

	vsprintf( pszBuffer, pszFormat, args );

	std::string strRet = pszBuffer;

	delete [] pszBuffer;

	return strRet;
}

Open in new window

This IMO is exactly what you asked for, it can be used like this:

    string s = myFormatFunction("%c%c%c%c%c", 'h', 'e', 'l', 'l', 'o');

Hope this helps,

ZOPPO
0
 
phoffricCommented:
In C++, you can just say:
string s = "hello";

In C, you can say:
char sArray[] = "hello";
or
char *sPtr = "hello";

>> There must already be a flavor of printf
Yes, sprintf "writes" to a buffer instead of a console. In their below example, "buffer" is being written to in a style similar to printf.
http://www.cplusplus.com/reference/cstdio/sprintf/?kw=sprintf
/* sprintf example */
#include <stdio.h>

int main ()
{
  char buffer [50];
  int n, a=5, b=3;
  n=sprintf (buffer, "%d plus %d is %d", a, b, a+b);
  printf ("[%s] is a string %d chars long\n",buffer,n);
  return 0;
}

Open in new window

0
 
n2fcCommented:
sprintf() effectively does a "print to a string"...

See: http://www.cplusplus.com/reference/cstdio/sprintf/
for a description...

Sounds like this is what you want?
0
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.

 
threadyAuthor Commented:
no- I want the string as the return value.
0
 
threadyAuthor Commented:
I know we can write a function to do that, but I was hoping it existed already...
0
 
threadyAuthor Commented:
That way, I never need to know a max buffer allocation length- it comes from my format string itself...
0
 
Tommy BraasCommented:
In plain C you must allocate a buffer yourself. What you can do is create a wrapper function to create your string and return it, where it calculates the overall length of the result char buffer before allocating the memory for it and printing to it.

Another option is to allocated a huge char buffer, one much larger than you would ever need, then print to it and substring it before returning.

I would suggest the first option.
0
 
phoffricCommented:
>> There must already be a flavor of printf that does this
Yes, but if you want to use the printf format, you do so with a char* allocated buffer or a char buffer array.
Below shows how to convert the char buffer array to a string.
You might be tempted to use sprintf on a string's c_str() function, but c_str() is used to get, not set, the underlying array. It also could be placed in read-only memory as c_str() returns a const char*.
http://www.cplusplus.com/reference/string/string/c_str/

/* sprintf example */
#include <iostream>
#include <string>
#include <stdio.h>


int main ()
{
  char buffer [50];
  int n, a=5, b=3;
  n=sprintf (buffer, "%d plus %d is %d", a, b, a+b);
  printf ("[%s] is a string %d chars long\n",buffer,n);
  
  std::string str(buffer);       // Convert the char array to a string
  std::cout << "[" << str << "] is a string " << str.size() << " chars long" << std::endl;
  
  return 0;
}

Open in new window


>> That way, I never need to know a max buffer allocation length
If that is your requirement, then you cannot use printf type formats.
0
 
threadyAuthor Commented:
Exactly what I wanted, thank you!
0
All Courses

From novice to tech pro — start learning today.