You are not logged in.

#1 2011-10-15 12:56:08

tony5429
Member
Registered: 2006-03-28
Posts: 1,025
Website

[SOLVED] Combining Printf with System

Right now I have the following code in a C++ project. The purpose is to invoke an external script with the myVar variable as an option.

char str[2000];
memset(str, 0, 2000);
sprintf(str, "sh myScript %i", myVar);
system(str);

I am wondering if there is any function that can return a formatted string, instead of outputting it to stdout (like printf), storing it to a C string (like sprintf), or writing it to a file (like fprintf). If there's a function like that (I'll call it xprintf), I could simplify my code into this one line.

system(xprintf("sh myScript %i", myVar));

Does anyone know if a function like that exists? If not, does anyone know how I could write the function?

Last edited by tony5429 (2011-11-09 04:40:34)

Offline

#2 2011-10-15 13:04:30

Nanthiel
Member
From: Slovenia
Registered: 2009-09-20
Posts: 148

Re: [SOLVED] Combining Printf with System

Such a function would introduce a memory leak in the program, as the string would be allocated but could never be freed, since no pointer to it would exist.

Offline

#3 2011-10-15 13:56:44

tony5429
Member
Registered: 2006-03-28
Posts: 1,025
Website

Re: [SOLVED] Combining Printf with System

Hrm, guess that makes sense. Is there any function that would send it to the shell directly, and then free up that RAM? Something like this?

sysprintf("sh myScript %i", myVar);

And if not, does anyone know how I could write that function?

Offline

#4 2011-10-15 15:50:08

Barrucadu
Member
From: York, England
Registered: 2008-03-30
Posts: 1,158
Website

Re: [SOLVED] Combining Printf with System

Rather than write a function, you could do it as a pretty simple (c99) macro:

#define sysprintf(...) do{ char* syscmd = calloc (sizeof (char), 2000); sprintf (syscmd, __VA_ARGS__); system (syscmd); free (syscmd); } while (0)

And then call it as such:

sysprintf ("sh myScript %i", myVar);

Warning: That's untested, and there is probably a far better way of doing it than by just allocating 2000 chars.

Last edited by Barrucadu (2011-10-15 15:51:55)

Offline

#5 2011-10-15 16:14:19

Nanthiel
Member
From: Slovenia
Registered: 2009-09-20
Posts: 148

Re: [SOLVED] Combining Printf with System

The GNU extension has a (non portable) function that allocates the string depending on how much space it needs. I forgot the name, but I'm sure you can search for it online. Still needs to be de-allocated, of course. smile

Offline

#6 2011-10-15 16:40:49

falconindy
Developer
From: New York, USA
Registered: 2009-10-22
Posts: 4,111
Website

Re: [SOLVED] Combining Printf with System

You're thinking of asprintf, which is a GNU extension, but its also available on the BSDs and OSX.

Offline

#7 2011-11-09 04:39:51

tony5429
Member
Registered: 2006-03-28
Posts: 1,025
Website

Re: [SOLVED] Combining Printf with System

Thanks, all for your help, with which I was able to come up with a suitable solution. If anyone finds this page and is looking for the solution, it has been implemented in rev 25 of Yellowcot, available here: http://code.google.com/p/yellowcot/sour … svn25&r=25

Offline

#8 2011-11-10 16:11:43

tavianator
Member
From: Waterloo, ON, Canada
Registered: 2007-08-21
Posts: 859
Website

Re: [SOLVED] Combining Printf with System

You should at least use vsnprintf() to make sure you don't overflow str.  Better yet, use vsaprintf() to allocate the string for you, or vsnprintf(NULL, 0, ...) to determine the needed size of the buffer.

Offline

Board footer

Powered by FluxBB