简体   繁体   中英

optarg always returns null

Trying to make the following c code work, but everytime I give it a file to returrn the size of it says the filename is null.

example command line that I've tried:

Question7 -h -t -f question8.c

regardless, it returns that optarg is null. I'm unsure why this is happening.

#include <stdio.h>
#include <getopt.h>
#include <sys/utsname.h>
#include <time.h>
#include <sys/stat.h>

int main(int argc, char **argv){
  char c;
  struct utsname uname_pointer;
  time_t time_raw_format;
  struct stat s;

  int hflag = 0;
  int tflag = 0;
  int fflag = 0;
  char *fvalue = NULL;
  int index;
  int check;

  opterr = 0;

  while ((check = getopt (argc, argv, "htf:")) != -1){
      switch (check) {
         case 'h':
            hflag = 1;
            break;
         case 't':
            tflag = 1;
            break;
         case 'f':
            fflag = 1;

            break;
       } 
  }
  if (hflag ==1 ) {
      uname (&uname_pointer);
      printf("Hostname = %s \n", uname_pointer.nodename);
  }

  if (tflag ==1 ){
     time (&time_raw_format);
     printf("the current local time: %s", ctime(&time_raw_format));
  }

  if (fflag == 1){
      if (stat(optarg, &s) == 0){
             printf("size of file '%s' is %d bytes\n", optarg, (int) s.st_size);
      }else {
             printf("file '%s' not found\n", optarg);
      }
  }
}

When you get -f (or 'f' ), that's when you read optarg :

char *fname = 0;

     case 'f':
        fname = optarg;
        break;

Etc. optarg is re-zeroed each time, so when the getopt() fails and you exit the loop, it is NULL once more. In general, you can have many options which take option values, and a single global variable cannot store them all at once.

#include <stdio.h>
#include <getopt.h>
#include <sys/utsname.h>
#include <time.h>
#include <sys/stat.h>

int main(int argc, char * *argv)
{
    char *fname = NULL;
    int   check;
    int   hflag = 0;
    int   tflag = 0;

    opterr = 0;

    while ((check = getopt(argc, argv, "htf:")) != -1)
    {
        switch (check)
        {
        case 'h':
            hflag = 1;
            break;
        case 't':
            tflag = 1;
            break;
        case 'f':
            fname = optarg;
            break;
        }
    }

    if (hflag == 1)
    {
        struct utsname uname_pointer;
        uname(&uname_pointer);
        printf("Hostname = %s \n", uname_pointer.nodename);
    }

    if (tflag == 1)
    {
        time_t time_raw_format;
        time(&time_raw_format);
        printf("the current local time: %s", ctime(&time_raw_format));
    }

    if (fname != NULL)
    {
        struct stat s;
        if (stat(fname, &s) == 0)
            printf("size of file '%s' is %d bytes\n", fname, (int) s.st_size);
        else
            printf("file '%s' not found\n", fname);
    }
    return 0;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM