Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #98871

Re: [command line arguments] more elegant solution ??

Newsgroups comp.lang.c
Date 2016-12-11 11:53 -0800
References (1 earlier) <87eg1exvi9.fsf@bsb.me.uk> <6c1666e7-6408-4700-a496-fc023c8c08f7@googlegroups.com> <df433c63-a960-4cbd-9a27-1554fd5378e7@googlegroups.com> <87k2b6wal9.fsf@bsb.me.uk> <9d6038e2-b9f4-474b-8c7c-71bee08459ac@googlegroups.com>
Message-ID <2be40ddd-6074-4db7-b2b3-ec40d33acf20@googlegroups.com> (permalink)
Subject Re: [command line arguments] more elegant solution ??
From Dexterr <dejangru@gmail.com>

Show all headers | View raw


On Sunday, December 11, 2016 at 8:15:19 PM UTC+1, Dexterr wrote:
> On Sunday, December 11, 2016 at 8:05:13 PM UTC+1, Ben Bacarisse wrote:
> > Dexterr <dejangru@gmail.com> writes:
> > 
> > > On Sunday, December 11, 2016 at 6:16:51 PM UTC+1, Dexterr wrote:
> > >> > (a) put all the checking into the checkInputArguments function;
> > >> 
> > >> Thanks Mr Ben on your response. I will do more changes. This is just first try or
> > >> first step to do all changes you suggest.
> > >> So, this part should go to this direction:
> > >> 
> > >> int checkInputArguments(char *argument[], int arg_counter)
> > >> {
> > >>   if(argc == 1)
> > >>   {
> > >>     printf("[ERR] usage: /ass.netlist omega\\n\n");
> > >>     return 1;
> > >>   }
> > >>   
> > >>   if(strcmp(str, argument[1]) != 0) 
> > >>   {
> > >>    printf("[ERR] usage: /ass.netlist omega\\n\n");
> > >>    return 1; 
> > >>   }
> > >>   else if(arg_counter != 3)
> > >>   {
> > >>     printf("[ERR] usage: /ass.netlist omega\\n\n");
> > >>     return 1;
> > >>   }
> > >>   return 0;
> > >> }
> > >> 
> > >> > (b) use its return value in main to decide what exit status the program has;
> > >> I don't understand this part.
> > >> 
> > >> >(c) put the error message in only one place rather than three; 
> > >> If I have a "if condition" I  only know to do on this way.
> > >
> > > ----
> > >
> > > - > Yes, the problem is, when I put in the same function from main, I got an "error message" 
> > > "error: use of undeclared identifier 'argc'"
> > >   if(argc == 1)
> > 
> > Yes, I missed that in my reply.  Apart from variables you declare inside
> > a function (none in this case), a function can only refer to it's
> > parameters (in this case 'argument' and 'arg_counter') and to previously
> > declared file scope objects (which many people call global variables).
> > Your program has one of these called 'str' and the function does indeed
> > refer to it.
> > 
> > But the name 'argc' is in neither of these categories.  In fact, you
> > pass the value of argc (a parameter of main) to the function precisely
> > so you can use that value inside the function -- but you must use the
> > parameter name you chose for it.
> > 
> > -- 
> > Ben.
> 
> Thanks Mr Ben on your response. This is my solution.
> - Your idea is genial to put return 1 in main function but
>   I don't know how to it but would be happy to put it as you suggest
> 
>  All other aspect is now better, I think. Here it is:
> 
> --
> 
> #include <stdio.h>
> #include <string.h>
> #include <stdlib.h>
> #include <ctype.h>
> 
> int checkInputArguments(char *argument[], int arg_counter);
> void printErrorMessage();
> 
> const char *str = "netlist";
> 
> 
> int main(int argc, char *argv[])
> {
>   
>   checkInputArguments(argv, argc);
>   return 0;
> }
> 
> int checkInputArguments(char *argument[], int arg_counter)
> {
>   
>   if(arg_counter == 1)
>   {
>     printErrorMessage();
>     return 1;
>   } 
>   else if(strcmp(str, argument[1]) != 0) 
>   {
>     printErrorMessage();
>     return 1; 
>   }
>   else if(arg_counter != 3)
>   {
>     printErrorMessage();
>     return 1;
>   }
>   // Checking does the 3rd argument is one integer or not
>   // else if(isdigit(**(argument + 2)) == 0)
>   else if(isdigit(*argument[2]) == 0)
>   {
>     printErrorMessage();
>     return 1;
>   }
>   return 0;
> }
> 
> void printErrorMessage()
> {
>   fprintf(stderr, "[ERR] usage: ./test netlist omega\n");
> }

Sorry, I have found solution in main function for "return 1".
Here it is:

int main(int argc, char *argv[])
{
  int check = checkInputArguments(argv, argc);
  if(check == 0)
    return 1;
  return 0;
}

---

It works perfect. Thanks for this help. I have learn a lot on just this example. 

Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

[command line arguments] more elegant solution ?? Dexterr <dejangru@gmail.com> - 2016-12-11 07:56 -0800
  Re: [command line arguments] more elegant solution ?? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2016-12-11 16:47 +0000
    Re: [command line arguments] more elegant solution ?? Dexterr <dejangru@gmail.com> - 2016-12-11 09:16 -0800
      Re: [command line arguments] more elegant solution ?? Dexterr <dejangru@gmail.com> - 2016-12-11 09:26 -0800
        Re: [command line arguments] more elegant solution ?? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2016-12-11 19:05 +0000
          Re: [command line arguments] more elegant solution ?? Dexterr <dejangru@gmail.com> - 2016-12-11 11:15 -0800
            Re: [command line arguments] more elegant solution ?? Dexterr <dejangru@gmail.com> - 2016-12-11 11:53 -0800
              Re: [command line arguments] more elegant solution ?? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2016-12-11 20:03 +0000
                Re: [command line arguments] more elegant solution ?? Dexterr <dejangru@gmail.com> - 2016-12-11 12:26 -0800
                Re: [command line arguments] more elegant solution ?? Dexterr <dejangru@gmail.com> - 2016-12-11 12:33 -0800
                Re: [command line arguments] more elegant solution ?? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2016-12-11 20:33 +0000
                Re: [command line arguments] more elegant solution ?? Dexterr <dejangru@gmail.com> - 2016-12-11 12:35 -0800
                Re: [command line arguments] more elegant solution ?? "James R. Kuyper" <jameskuyper@verizon.net> - 2016-12-11 16:06 -0500
                Re: [command line arguments] more elegant solution ?? Dexterr <dejangru@gmail.com> - 2016-12-11 13:11 -0800
                Re: [command line arguments] more elegant solution ?? Dexterr <dejangru@gmail.com> - 2016-12-11 13:20 -0800
                Re: [command line arguments] more elegant solution ?? Dexterr <dejangru@gmail.com> - 2016-12-11 13:25 -0800
                Re: [command line arguments] more elegant solution ?? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2016-12-11 21:32 +0000
                Re: [command line arguments] more elegant solution ?? Dexterr <dejangru@gmail.com> - 2016-12-11 13:44 -0800
                Re: [command line arguments] more elegant solution ?? Dexterr <dejangru@gmail.com> - 2016-12-11 13:47 -0800
                Re: [command line arguments] more elegant solution ?? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2016-12-11 21:56 +0000
                Re: [command line arguments] more elegant solution ?? Dexterr <dejangru@gmail.com> - 2016-12-11 14:04 -0800
                Re: [command line arguments] more elegant solution ?? Robert Spanjaard <spamtrap@arumes.com> - 2016-12-11 23:07 +0100
                Re: [command line arguments] more elegant solution ?? Dexterr <dejangru@gmail.com> - 2016-12-11 14:15 -0800
                Re: [command line arguments] more elegant solution ?? Robert Spanjaard <spamtrap@arumes.com> - 2016-12-11 23:32 +0100
                Re: [command line arguments] more elegant solution ?? jameskuyper@verizon.net - 2016-12-11 14:42 -0800
                Re: [command line arguments] more elegant solution ?? Dexterr <dejangru@gmail.com> - 2016-12-11 14:52 -0800
                Re: [command line arguments] more elegant solution ?? Dexterr <dejangru@gmail.com> - 2016-12-11 14:40 -0800
                Re: [command line arguments] more elegant solution ?? jameskuyper@verizon.net - 2016-12-11 14:55 -0800
                Re: [command line arguments] more elegant solution ?? "James R. Kuyper" <jameskuyper@verizon.net> - 2016-12-11 19:44 -0500
                Re: [command line arguments] more elegant solution ?? Richard Heathfield <rjh@cpax.org.uk> - 2016-12-11 23:20 +0000
                Re: [command line arguments] more elegant solution ?? Dexterr <dejangru@gmail.com> - 2016-12-11 15:28 -0800
                Re: [command line arguments] more elegant solution ?? jameskuyper@verizon.net - 2016-12-11 16:28 -0800
      Re: [command line arguments] more elegant solution ?? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2016-12-11 18:57 +0000
  Re: [command line arguments] more elegant solution ?? Barry Schwarz <schwarzb@dqel.com> - 2016-12-11 10:04 -0800
    Re: [command line arguments] more elegant solution ?? Dexterr <dejangru@gmail.com> - 2016-12-11 11:25 -0800
  Re: [command line arguments] more elegant solution ?? BartC <bc@freeuk.com> - 2016-12-11 18:29 +0000
    Re: [command line arguments] more elegant solution ?? Dexterr <dejangru@gmail.com> - 2016-12-11 11:25 -0800
    Re: [command line arguments] more elegant solution ?? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2016-12-11 20:30 +0000
      Re: [command line arguments] more elegant solution ?? BartC <bc@freeuk.com> - 2016-12-11 23:43 +0000
        Re: [command line arguments] more elegant solution ?? Lew Pitcher <lew.pitcher@digitalfreehold.ca> - 2016-12-11 19:02 -0500
          Re: [command line arguments] more elegant solution ?? Lew Pitcher <lew.pitcher@digitalfreehold.ca> - 2016-12-11 19:13 -0500
            Re: [command line arguments] more elegant solution ?? BartC <bc@freeuk.com> - 2016-12-12 00:55 +0000
              Re: [command line arguments] more elegant solution ?? BartC <bc@freeuk.com> - 2016-12-12 01:02 +0000
                Re: [command line arguments] more elegant solution ?? Keith Thompson <kst-u@mib.org> - 2016-12-11 19:02 -0800
                Re: [command line arguments] more elegant solution ?? BartC <bc@freeuk.com> - 2016-12-12 11:50 +0000
                Re: [command line arguments] more elegant solution ?? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2016-12-12 13:46 +0000
                Re: [command line arguments] more elegant solution ?? BartC <bc@freeuk.com> - 2016-12-12 14:43 +0000
                Re: [command line arguments] more elegant solution ?? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2016-12-12 22:09 +0000
                Re: [command line arguments] more elegant solution ?? Keith Thompson <kst-u@mib.org> - 2016-12-12 08:58 -0800
              Re: [command line arguments] more elegant solution ?? Lew Pitcher <lew.pitcher@digitalfreehold.ca> - 2016-12-11 20:06 -0500
              Re: [command line arguments] more elegant solution ?? jameskuyper@verizon.net - 2016-12-11 17:37 -0800
              Re: [command line arguments] more elegant solution ?? Keith Thompson <kst-u@mib.org> - 2016-12-11 18:59 -0800
        Re: [command line arguments] more elegant solution ?? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2016-12-12 02:47 +0000
  Re: [command line arguments] more elegant solution ?? Dexterr <dejangru@gmail.com> - 2016-12-11 15:11 -0800
  Re: [command line arguments] more elegant solution ?? Dexterr <dejangru@gmail.com> - 2016-12-12 00:25 -0800
  Re: [command line arguments] more elegant solution ?? Dexterr <dejangru@gmail.com> - 2016-12-12 00:27 -0800
    Re: [command line arguments] more elegant solution ?? Barry Schwarz <schwarzb@dqel.com> - 2016-12-12 01:01 -0800
      Re: [command line arguments] more elegant solution ?? Dexterr <dejangru@gmail.com> - 2016-12-12 09:42 -0800
        Re: [command line arguments] more elegant solution ?? "James R. Kuyper" <jameskuyper@verizon.net> - 2016-12-12 13:58 -0500
        Re: [command line arguments] more elegant solution ?? Barry Schwarz <schwarzb@dqel.com> - 2016-12-12 14:18 -0800

csiph-web