You are not logged in.

#1 2008-03-05 17:56:37

tony5429
Member
Registered: 2006-03-28
Posts: 1,017

Very Simple C question - reading input parameters/flags... [SOLVED]

This is an extremely simple question. Please forgive me for being a noob. I am writing an application in C (with GCC) called GAPE and need to be able to pass it parameters through a shell script (/usr/bin/gape) that can determine how it runs. To start with easy stuff, I want the end-user to be able to type "gape -V" to output the version of the program. How do I do that? I understand that I need to put the shell script in /usr/bin...where should I put the actual gcc-compiled gape application? Or do I even need a shell script? Can I just put the GAPE binary in /usr/bin and pass parameters to it directly? If so, how do I do that in C? Any help is greatly appreciated.

Last edited by tony5429 (2008-03-10 12:00:21)

Offline

#2 2008-03-05 18:04:16

JaQoB
Member
Registered: 2007-04-04
Posts: 60

Re: Very Simple C question - reading input parameters/flags... [SOLVED]

include <stdio.h>

main(int argc,  char *argv[])
{ 
int i;

    for(i = 1; i < argc; i++) //argc = the number of arguments
    {
            printf("param nr %d: %s\n", i, argv[i]); // argv[i] contain the arguments, with 0 being the program name (anyone may correct me here)
    }
}
[jaqob@p238 c++-egna]$ ./a.out 1 2 3
param nr 1: 1
param nr 2: 2
param nr 3: 3
[jaqob@p238 c++-egna]$ ./a.out -h -V -zxvf
param nr 1: -h
param nr 2: -V
param nr 3: -zxvf

A very simple program that prints the parameters
And yes, you can put the program in /usr/bin/ without a shellscript smile

Last edited by JaQoB (2008-03-05 18:09:34)

Offline

#3 2008-03-05 18:09:50

tony5429
Member
Registered: 2006-03-28
Posts: 1,017

Re: Very Simple C question - reading input parameters/flags... [SOLVED]

Thanks! I will give it a shot!

Offline

#4 2008-03-06 00:58:13

elgatofelix
Member
From: Chile
Registered: 2007-07-03
Posts: 137

Re: Very Simple C question - reading input parameters/flags... [SOLVED]

argv[0] isn't fixed to the program name, it could be any string that the caller pass as the first arg. The shell passes
the program name but u can call a program with a exec* system call and pass whatever u want to for the 0 argument


Are u listening?

Offline

#5 2008-03-06 07:58:21

JaQoB
Member
Registered: 2007-04-04
Posts: 60

Re: Very Simple C question - reading input parameters/flags... [SOLVED]

elgatofelix wrote:

argv[0] isn't fixed to the program name, it could be any string that the caller pass as the first arg. The shell passes
the program name but u can call a program with a exec* system call and pass whatever u want to for the 0 argument

Thanks for the correction smile

Offline

#6 2008-03-07 22:44:49

SiC
Member
From: Liverpool, England
Registered: 2008-01-10
Posts: 430

Re: Very Simple C question - reading input parameters/flags... [SOLVED]

If you are using C, look at the getopt and getopt_long functions. They simplify the handling of options greatly.

Offline

#7 2008-03-10 02:49:25

tony5429
Member
Registered: 2006-03-28
Posts: 1,017

Re: Very Simple C question - reading input parameters/flags... [SOLVED]

Thanks for all the support! Now that I've done some work on the program, I am running into another problem. Here is my code...

long x;
x = (long)strchr("hello/bye", 47);
printf("x: %d\n", x);

47 is the ASCII code of '/' so I figure I should get the pointer, '5' for the character's location in the string but instead I seem to get 4196713 every time...

Any ideas?

Offline

#8 2008-03-10 03:46:03

Allan
Pacman
From: Brisbane, AU
Registered: 2007-06-09
Posts: 11,383
Website

Re: Very Simple C question - reading input parameters/flags... [SOLVED]

strchr returns a char* to the match.  Example usage:

#include <stdio.h>
#include <string.h>

int main ()
{
  char str[] = "This is a sample string";
  char * pch;
  printf ("Looking for the 's' character in \"%s\"...\n",str);
  pch=strchr(str,'s');
  while (pch!=NULL)
  {
    printf ("found at %d\n",pch-str+1);
    pch=strchr(pch+1,'s');
  }
  return 0;
}

Offline

#9 2008-03-10 12:00:03

tony5429
Member
Registered: 2006-03-28
Posts: 1,017

Re: Very Simple C question - reading input parameters/flags... [SOLVED]

Once again, thank you very much! It is exactly what I needed.

Offline

Board footer

Powered by FluxBB