473,386 Members | 2,078 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Retrieving the output of a C program using a PHP script

I have a problem, which I think can be easily solved somehow.

I am trying to execute a C program through using a PHP script. I do
this by sending an exec command from the PHP script. My PHP script is
printed below:

<?php

system('main.exe');

?>

When I execute my PHP script, it only prints 'Hello World', and not
the output of the buffer in the C program. My C code is shown below:

#include <stdio.h>

int main()
{
printf("Hello World\n"); /* this prints fine on the php script */

char buffer[1024];

FILE *pipe;
pipe = popen("php -f testing.php", "r"); /* executing the php file
'testing.php' via the command line */
fread(buffer, 1, 1024, pipe);
printf("%s", buffer); /* this prints the output of the execution of
the PHP script */
pclose(pipe);

return 0;
}
Why does it do this? Is there a way around this issue?

If you execute the C program via the command line, the output of the
PHP script prints fine.

Any help with this issue would be most appreciated.

Many Regards,
Ben Chivers
Nov 14 '05 #1
2 3154
begin followup to Ben Chivers:
FILE *pipe;
pipe = popen("php -f testing.php", "r");
fread(buffer, 1, 1024, pipe);
Oops. Evil. What of popen failes and returns zero?
printf("%s", buffer);
Ugh. Not only is this lame, it's also wrong.
Who says that the output of a PHP-script will be 0-terminated?
fread itself will definitely not append the zero.

pipe = popen(...);
if (pipe != 0)
{
size_t len = fread(buffer, 1, sizeof(buffer), pipe);
fwrite(buffer, 1, len, stdout);
pclose(pipe);
}

For debugging purposes you might also print the value of 'len'.
[...]
Any help with this issue would be most appreciated.


Get a clue. Seriously. You seem to program by trial-and-error.
Read the manual instead, check return values, try to think of ways
it can possible go wrong.

Do you know the absolute path of php.exe? Why not?
How does the shell (cmd.exe/command.com) find it?
Does your web-server provide the same environment to CGIs?
What about dumping _environ? Or at least selective getenv?

--
Für Google, Tux und GPL!
Nov 14 '05 #2
Alexander Bartolich <al*****************@gmx.at> wrote in
news:bt************@ID-193444.news.uni-berlin.de:
begin followup to Ben Chivers:
FILE *pipe;
pipe = popen("php -f testing.php", "r");
fread(buffer, 1, 1024, pipe);
Oops. Evil. What of popen failes and returns zero?


What if popen() isn't part of C?
printf("%s", buffer);


Ugh. Not only is this lame, it's also wrong.
Who says that the output of a PHP-script will be 0-terminated?
fread itself will definitely not append the zero.


Also, we need to ensure that we terminate with a '\n'.
[snip]

--
- Mark ->
--
Nov 14 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: PinkGuava | last post by:
Hi, I have a T-SQL stored procedure that returns both output parameters and a recordset. How do I retrieve them in my ASP script? As far as I know, the ADO Command object can be used to retrieve...
1
by: Steve | last post by:
Hi; Before I start let me say I know this is a silly way to go about things, but it is one of those "my company made me do it" things. My company is in the process of ( finishing ) migrating...
5
by: Carl Castrianni | last post by:
When running files containing many sql statements using the db2 command line processor, I`d like to have the output show the time that each sql statement was executed (so I know when they ran). ...
0
by: Andy | last post by:
Hi All. I'm working for a company that has set out a guideline for retrieving data from a database. Nobody can explain to me the reason for the following. When retrieving a set of records...
7
by: rfinch | last post by:
Very new to this but using the MS working with dynamics CRM 3.0 book to run web application to retrieve lead records from CRM 3.0. Have followed the book instructions on page 380-382. But am...
3
by: Madmartigan | last post by:
Hello I have the following task but am battling with the final output. How do I keep two different vectors in sync and how would I retrieve the index for the maximum value of one of the vectors??...
2
by: gopala | last post by:
Hi, I am pretty new to python but i do have experience with c++. As a part of learning exercise i wrote a python script to insert beautifying comments(with name, problem...) to our lab program...
0
bmallett
by: bmallett | last post by:
First off, i would like to thank everyone for any and all help with this. That being said, I am having a problem retrieving/posting my dynamic form data. I have a form that has multiple options...
4
by: DiffThinkr | last post by:
Hi, Is there anyway of saving and retrieving the output screen. I don't want to use the saved file outside the program but I need to be able to put the output that I saved back into output as it...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.