Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

C++ CGI

Status
Not open for further replies.

techxx

MIS
Jul 20, 2006
5
US
Not sure if anyone can help me, but as a novice C++ programmer (it's been 9yrs since I wrote a C++ program in college)I am seeking some answers to a problem at work.
We are trying to call a perl program, on our unix box, from a C++ program. The perl program requires us to pass a parameter via C++. After executing the perl program it will retun data string to the C++ program. My problem is I don't know how to execute the perl program from C++. I would imagine this would require some CGI coding. Can any one help me, or direct me to a tutorial on the web?
 
to execute perl from C
Code:
   system ("perl whatever.pl parameter");
Alternatively, you can sprintf to a char array and pass it as the parameter to system.
 
After executing the perl program it will retun data string to the C++ program.
I've never written a CGI program before. How does it return data to the C++ program? The only thing I can think of is that it prints the data to stdout...
 
You right. I don't think I would be able to return a value to the C program via CGI. However I found a web site on calling a Perl subroutine from your C Program. I think this is what I'm looking for. I also think it should work for C++ as well. If your interested take a look.
 
You typically don't use CGI except when writing a web program. If you want to retrieve the output of the perl script, use the popen() function. It will allow you to access the output of the file with normal file operations:

Code:
FILE *perl;
char buf[1024];
int bytes;

perl = popen("perl whatever.pl params", "r");
if (perl) {
  bytes = fread(buf, 1024, 1, perl);
  if (bytes >= 0)
    buf[bytes] = '\0';
}
pclose(perl);

--
 
Thanks! As I said I'm not real good with C++, but my job requires me to pass a param to the perl code and retrieve the output. I'll look at the code excerpt you have, and try to figure it all out.
I looked at the embedding perl in C++ and it may be for a more experienced programmer. If anybody has any other solutions I would appreciate it.
 
I want to be able to pass a SQL statement I'm generating in C++ to the perl program. Can I pass this to the perl program using the popen function? If so how?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top