943,645 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 1816
  • C RSS
Sep 24th, 2008
0

getopt - help

Expand Post »
Hi like to write a program that can take arguments like %program -t <type> <path> <path> <name>

where -t follow by type is the file type I like to look for in a find program, and path's is where I want to look, followed by the name of the file I like to search for....

like

%program -t f (file) ~testfolder1 ~testfolder2 help

The problem is I don't know how to get getopt to read -t and then take next char as an argument to look for S_ISREG(m) for example..

Would be great with a lite help, will try to update the thread later on with code if someone have same problem..

  1. #include <stdio.h>
  2. #include <unistd.h>
  3.  
  4. int main (int argc, char *argv[]){
  5.  
  6. int c;
  7. char *path, filetype;
  8. extern char *optarg;
  9. extern int optind, optopt, opterr;
  10.  
  11. while ((c = getopt(argc, argv, "t")) != -1){
  12.  
  13. switch(c){
  14.  
  15. case 't':
  16. filetype = ????; // What the *** should be here? :D
  17. printf("Type you are looking for is:
  18. %c\n", filetype);
  19. printf("option: %c\n",c);
  20. break;
  21.  
  22. case '?':
  23. fprintf(stderr, "Unrecognised option:
  24. -%c\n",optopt);
  25. }
  26. }
  27. // This is just for listing witch arguments there is..
  28. for(i=1;i<argc;i++){
  29. printf("argv: %s\n",argv[i]);
  30. }
  31.  
  32. return 0;
  33.  
  34. }
Similar Threads
Reputation Points: 10
Solved Threads: 1
Newbie Poster
TooMpa is offline Offline
7 posts
since Sep 2008
Sep 24th, 2008
0

Re: getopt - help

As far as I know you need "t:" getopt option. May be I'm wrong (I don't like getopt ).
See http://www.gnu.org/software/libtool/...l#Using-Getopt
Reputation Points: 1234
Solved Threads: 347
Postaholic
ArkM is offline Offline
2,001 posts
since Jul 2008
Sep 25th, 2008
0

Re: getopt - help

Yeah it should be "t:" and if t is an optional argument it should be "t::"

why don't you like getopt ?
Reputation Points: 161
Solved Threads: 43
Posting Whiz
stilllearning is offline Offline
309 posts
since Oct 2007
Sep 27th, 2008
0

Re: getopt - help

Nothing serious. I don't like a rigid and unclear (in my opinion) getopt loop (and option string too ). It's a matter of personal preferences only.
Probably the getopt is the best solution in C, but in C++ it looks like a steam-engine in Tokkaido Line train... But it's the other story...
Reputation Points: 1234
Solved Threads: 347
Postaholic
ArkM is offline Offline
2,001 posts
since Jul 2008
Sep 28th, 2008
0

Re: getopt - help

Quote ...
filetype = ????;
So if the flag matches, you can get the argument by the global variable optarg. That should hold pointer to the argument which you are looking for!

  1. filetype = optarg;

ssharish
Reputation Points: 73
Solved Threads: 20
Posting Whiz in Training
ssharish2005 is offline Offline
253 posts
since Dec 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: Frames in C ...
Next Thread in C Forum Timeline: Need help turn this into a recursive function





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC