473,698 Members | 2,571 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.ex e');

?>

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 3164
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
9488
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 the output parameters, but will I be able to retrieve the recordset using the Command object as well? Or do I have to use the Recordset object? Example of stored procedure:
1
2942
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 from foxpro to sqlserver. They have a foxpro program that does a lot of various updates. I would like to write a tsql script to replace it.
5
2685
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). Something like: 10:05:00 Select count(*) from ..... 10:05:30 Select count(*) from ..... Is there a way to do this? I know db2batch can do this (with SET TIMESTAMP) but I`d rather use db2 clp.
0
2071
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 from database to a VB.net app, they retrieve the database fields as a record set eg. "select name, suburb from myTable"
7
4701
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 getting the following in ie 7. Compiler Error Message: CS0234: The type or namespace name 'CrmSdk' does not exist in the class or namespace 'WorkingWithCrm' (are you missing an assembly reference?)
3
17921
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?? (using Dev-C++ compiler) Please see task and attempt below: TASK : 1.8.1 Exercise 1A - Temperature tracker Write a program that tracks temperatures during an experiment that takes 24 hours. The user should input the temperature. This...
2
1573
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 source files (c language). The script is working for most of the cases. The problem is i also need to append the test output of the lab programs to the corresponding source files(again the output should be added as a beautified comment). The lab...
0
3385
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 within options. I have everything being dynamically named from the previously dynamically named element. (I hope this makes sense.) I am not able to retrieve any of the dynamically created values. I can view them on the source page but can't pull them...
4
2938
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 is. This is for a graphics application, by the way. I'm using Turbo c++.
0
8683
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8610
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9031
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8873
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6528
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5862
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4623
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3052
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2339
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.