I am writing a C program that part of the idea is to using a command line parameter to control not to run certain part of the sub program.
I am totally new to C, I do not have any idea how to pass a command line arguments from a C program.
Can anyone help ?!
Thanks
You can try to declare main with arguments;
int main(int argc, char *argv[])
This program was used in an application, where a PC was controlling critical tasks. Lets call it PC_critical. Unfortunately, PC_critical occasionally stalled and had to be manually reset.
Thus, a second PC (PC_monitor) was used. PC_critical was to send a message to PC_monitor every five minutes to assure that it was "alive". If no message was received, PC_monitor forced a hard reset of PC_critical by applying a momentary relay closure across the manual reset switch on PC_critical. The operation of the relay was caused by outputting the pattern 0x0a on the Data Port.
You can try this as an example.
#include <stdio.h>
#include <conio.h>
#include <dos.h>
#include <process.h>
int main(int argc, char *argv[])
{
unsigned int t_delay, Data;
int tenths_sec;
clrscr();
if(argc != 3)
{
printf("Incorrect number of args\n");
exit(-1);
}
if(sscanf(argv[1],"%d", &tenths_sec)!=1)
{
printf("Time scanf failed.\n");
exit(-2);
}
if (sscanf(argv[2],"%x", &Data) != 1)
{
printf("Parallel port scanf failed.\n");
exit(-3);
}
t_delay = tenths_sec * 100;
outportb(Data, 0x0a); /* operate the relay */
delay(t_delay);
outportb(Data, 0x00); /* release it */
exit(0);
}
Hi,
I have some additional infor for you. There is one more arg to main. It is the char *env[]. which is very similar to char *argv[]. This arg contains all the system variables set up in the environment. Also there is a workaround for the same. you also have a getenv function which can help you get these arguments.
Happy programming buddy
penguin