# c++ a simple question.



## highspider (Apr 24, 2011)

I don't know what this is called, but here's what I'm trying to do:


```
myfunc(const_charstr [color="DarkRed"]+ "sometext[/color]",arg2)

--example--
const char * MAIL_PROGRAM = "/usr/sbin/sendmail";
//...
//one of many failed trys
mail = popen (MAIL_PROGRAM [color="Red"]+ " -t"[/color], "w");
```

I use the char* for other things also so I want to add the -t opt there

1) how to do it 
2) what is it called


----------



## kpa (Apr 24, 2011)

Leave the + out, in C and C++ literal strings are concatenated if you write them one after another with just whitespace in between.


```
mail = popen (MAIL_PROGRAM " -t", "w");
```


----------



## highspider (Apr 24, 2011)

*Still no go*

I've tried with char, char * cstring and string


```
string MAIL_PROGRAM = "/usr/sbin/sendmail";  
char * MAIL_PROGRAM = "/usr/sbin/sendmail";  
char[] MAIL_PROGRAM = "/usr/sbin/sendmail";  

mail = popen (MAIL_PROGRAM " -t", "w");
 
mail = popen (MAIL_PROGRAM.c_str() " -t", "w");
```

It does work with a literal string but I need my var.


```
mail = popen ("/usr/sbin/sendmail" " -t", "w");
```


----------



## Alt (Apr 24, 2011)

C++ std::string can do it for you

```
string s1="/usr/sbin/sendmail";
string s2=" -t";
cout << "opening " << s1+s2 <<endl;
s2 = s1+s2;
popen(s2.c_str(), "w");
```


----------



## xibo (Apr 24, 2011)

The C way:


```
FILE *handle;
char  name[SUFFICIENT_SIZE + 1];
char *string1;
char *string2;

/* ... */
string1 = "/usr/bin/sendmail";
string2 = "-t"; /* leading space character would trouble the end user */
/* ... */

/* check we have enough memory first, add one character more for
   the space we need */
if( SUFFICIENT_SIZE >= strlen(string1) + strlen(string2) + 1 )
{
  /* copy string from string1 into name. strcpy is defined in string.h */
  strcpy( name, string1 ); 
  
  /* concatenate (append) space character to name. strcat defined in string.h */
  strcat( name, " " );

  /* append string2 to name. */
  strcat( name, string2 );
}
else
{
  /* worthless error message */
  fprintf( stderr, "%s:%d: string too long!\n", __FILE__, __LINE__ );

  /* abort is more usefull for debugging then exit, both defined in stdlib.h */
  abort(); 
}

handle = popen( name, "w" );
/* ... */
```

Many people will prefer sprintf(3) of stdio.h here (especially since there's fprintf(3) elsewhere already), which does the work in one line (replaces the then-part of the if statement):

```
sprintf( name, "%s %s", string1, string2 );
```

The C++ way was already described by Alt.


----------



## highspider (Apr 24, 2011)

*Thanks guys*

Well that works. Thanks for Helping! I was hoping it could be done while passing to a func. But I will change program to account for an extra var.

By the way what is the term ("it called") when you do stuff in a func's passing of its arguments. What would the correct term for the 6+5 in test(5+6). I know the its addition but is like ergo pre-phrasing arguments or some fancy name.


```
#include <iostream>
void test(int num_test);
int main()
{
 int num_test = 0;
 test (5+6);
}
void test (int num_test)
{
std::cout << num_test;
}
```


----------



## Alt (Apr 24, 2011)

5+6 will be calculated at compile time. Its calling "constant-expression"
http://stackoverflow.com/questions/...d-at-compile-time-or-at-runtime/436678#436678


----------



## expl (Apr 24, 2011)

So much code for something so simple?

Here is how you do it without a fuss:


```
#define MAIL_PROGRAM "/usr/sbin/sendmail"

mail = popen (MAIL_PROGRAM " -t", "w");
```

or if you must use variables:


```
char *arg, MAIL_PROGRAM[] = "/usr/sbin/sendmail";

mail = popen ((asprintf(&arg, "%s -t", MAIL_PROGRAM) == -1) ? NULL : arg, "w");	
free(arg);
```


----------



## highspider (Apr 24, 2011)

expl said:
			
		

> So much code for something so simple?
> 
> Here is how you do it without a fuss:
> 
> ...



Thank you. I attached the full source, it's just an email.cgi but I'm new to cgi and haven't graduated college yet.


----------

