Accessing An External Programm

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

User avatar
neel_basu
Forum Contributor
Posts: 454
Joined: Wed Dec 06, 2006 9:33 am
Location: Picnic Garden, Kolkata, India

Post by neel_basu »

Suppose I Want to Make A C Programm That Will Take An Integer Through Commandline And Out Put That Integer*5 And I'll Pass That Int Through php And Get The Answer Through php Then What Would Be The C Code To Build That Program ??
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

Code: Select all

#include <stdio.h>
#include <stdlib.h>

int main(int argc,char *argv[])
{
	if ( argc > 1 ) {
		long x = strtol(argv[1], NULL, 10);
		printf("%ld", x*5);
	}
	return 0;
}
Can a moderator please move this thread to Miscellaneous?
User avatar
neel_basu
Forum Contributor
Posts: 454
Joined: Wed Dec 06, 2006 9:33 am
Location: Picnic Garden, Kolkata, India

Post by neel_basu »

Would You Please Tell Me How Does It Works ??
And How I Have To Pass That Integer Argument To It ??
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

Arguments are available as an array of (zero terminated) strings to a C program.
argv[1] is the first parameter/argument passed to the program, e.g. yadda.exe firstArgument -> argv[1]<=>firstArgument
To convert a string to a number you may use strtol, see http://msdn2.microsoft.com/en-us/w4z2wdyc(VS.80).aspx or your local msdn help.
User avatar
neel_basu
Forum Contributor
Posts: 454
Joined: Wed Dec 06, 2006 9:33 am
Location: Picnic Garden, Kolkata, India

Post by neel_basu »

Hello Sir
I Am Very Sorry To Post It In THis Forum But While Posting I Didnt Know
That The Topic Will Turn Like This


Would You Please tell Me How Can I Pass Two Arguments To That Program ?
Whats The Separator Is it Space Or comma
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

volka wrote:the arguments are separated by whitespaces.
yadda.exe firstArgument secondArgument
access them via argv[1] and argv[2]
User avatar
neel_basu
Forum Contributor
Posts: 454
Joined: Wed Dec 06, 2006 9:33 am
Location: Picnic Garden, Kolkata, India

Post by neel_basu »

Then Whats The Work Of argc What Does It Do ??
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

argument counter, the number of arguments.
User avatar
neel_basu
Forum Contributor
Posts: 454
Joined: Wed Dec 06, 2006 9:33 am
Location: Picnic Garden, Kolkata, India

Post by neel_basu »

How Can I Separate 2 or 3 arguments By Spaces Or By commas ??
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

:?:
User avatar
neel_basu
Forum Contributor
Posts: 454
Joined: Wed Dec 06, 2006 9:33 am
Location: Picnic Garden, Kolkata, India

Post by neel_basu »

argv[1] is Assidned To The First Argument I Send
argv[2] is Assigned To The Second Argument I Send

Suppose My First Argument Is arg1 And Second Argument Is arg2
how Would I Write This ??

Like This

Code: Select all

pass_arg.exe arg1 arg2
Or In some Other Way !!
User avatar
ok
Forum Contributor
Posts: 393
Joined: Wed May 31, 2006 9:20 am
Location: The Holy Land

Post by ok »

There are some rules for passing arguments to a program. One of them is that the arguments have to be separated by signal space.

If you want to separate them by commas, you can do it, but in the program you will have to separate them (C won't do it for you!)
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

a) already answered
volka wrote:yadda.exe firstArgument secondArgument
b) This one you really could've tried yourself.
User avatar
neel_basu
Forum Contributor
Posts: 454
Joined: Wed Dec 06, 2006 9:33 am
Location: Picnic Garden, Kolkata, India

Post by neel_basu »

So What is The Default Separator ??
User avatar
neel_basu
Forum Contributor
Posts: 454
Joined: Wed Dec 06, 2006 9:33 am
Location: Picnic Garden, Kolkata, India

Post by neel_basu »

Sorry

Ok So How Can I Separate The Input Arguments By a Comma ??
Post Reply