The UNIX and Linux Forums  


Go Back   The UNIX and Linux Forums > Top Forums > High Level Programming
.
google unix.com



High Level Programming Post questions about C, C++, Java, SQL, and other programming languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Using of Top command as script parameter elthox Shell Programming and Scripting 4 01-31-2007 01:21 PM
replacing a string in a file with command line parameter chiru_h Shell Programming and Scripting 5 08-03-2006 10:34 AM
pass parameter back to calling program jthomas Shell Programming and Scripting 11 11-20-2003 08:13 AM
using tab to finish command line parameter kymberm Shell Programming and Scripting 3 09-20-2002 04:54 PM
Command Line width parameter Scoogie UNIX for Dummies Questions & Answers 2 02-08-2002 05:04 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 05-15-2002
Wing m. Cheng Wing m. Cheng is offline
Registered User
  
 

Join Date: Jul 2001
Location: Hong Kong
Posts: 11
Question Command line parameter for C program

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
  #2 (permalink)  
Old 05-15-2002
killerserv's Avatar
killerserv killerserv is offline Forum Advisor  
Unix Predator
  
 

Join Date: Dec 2000
Location: Phoenix, AZ
Posts: 670
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.


Code:

#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);
}

  #3 (permalink)  
Old 05-16-2002
Wing m. Cheng Wing m. Cheng is offline
Registered User
  
 

Join Date: Jul 2001
Location: Hong Kong
Posts: 11
Thumbs up

Thanks Killerserv !!
  #4 (permalink)  
Old 05-18-2002
linuxpenguin's Avatar
linuxpenguin linuxpenguin is offline Forum Advisor  
Registered User
  
 

Join Date: May 2002
Location: India
Posts: 295
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
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 07:00 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0