c++ a simple question.

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

Code:
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
 
Leave the + out, in C and C++ literal strings are concatenated if you write them one after another with just whitespace in between.

Code:
mail = popen (MAIL_PROGRAM " -t", "w");
 
Still no go

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

Code:
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.

Code:
mail = popen ("/usr/sbin/sendmail" " -t", "w");
 
C++ std::string can do it for you
Code:
string s1="/usr/sbin/sendmail";
string s2=" -t";
cout << "opening " << s1+s2 <<endl;
s2 = s1+s2;
popen(s2.c_str(), "w");
 
The C way:

Code:
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):
Code:
  sprintf( name, "%s %s", string1, string2 );

The C++ way was already described by Alt.
 
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.

Code:
#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;
}
 
So much code for something so simple?

Here is how you do it without a fuss:

Code:
#define MAIL_PROGRAM "/usr/sbin/sendmail"

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

or if you must use variables:

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

mail = popen ((asprintf(&arg, "%s -t", MAIL_PROGRAM) == -1) ? NULL : arg, "w");	
free(arg);
 
expl said:
So much code for something so simple?

Here is how you do it without a fuss:

Code:
#define MAIL_PROGRAM "/usr/sbin/sendmail"

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

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.
 

Attachments

Back
Top