title: Parsing command-line arguments in C
body: <p>I'm trying to write a program that can compare two files line by line, word by word, or character by character in C. It has to be able to read in command line options <code>-l</code>, <code>-w</code>, <code>-i</code> or <code>--</code>...</p>
  <ul>
  <li>if the option is <code>-l</code>, it compares the files line by line.</li>
  <li>if the option is <code>-w</code>, it compares the files word by word.</li>
  <li>if the option is <code>--</code>, it automatically assumes that the next argument is the first filename.</li>
  <li>if the option is <code>-i</code>, it compares them in a case insensitive manner.</li>
  <li>defaults to comparing the files character by character.</li>
  </ul>
  <p>It's not supposed to matter how many times the options are input as long as <code>-w</code> and <code>-l</code> aren't inputted at the same time and there are no more or less than two files.</p>
  <p>I don't even know where to begin with parsing the command line arguments.</p>
  <p>So this is the code that I came up with for everything. I haven't error checked it quite yet, but am I writing things in an overcomplicated manner?</p>
  <pre><code>/*
   * Functions to compare files.
   */
  int compare_line();
  int compare_word();
  int compare_char();
  int case_insens();
  
  /*
   * Program to compare the information in two files and print message saying
   * whether or not this was successful.
   */
  int main(int argc, char* argv[])
  {
      /* Loop counter */
      size_t i = 0;
  
      /* Variables for functions */
      int caseIns = 0;
      int line = 0;
      int word = 0;
  
      /* File pointers */
      FILE *fp1, *fp2;
  
      /*
       * Read through command-line arguments for options.
       */
      for (i = 1; i &lt; argc; i++)
      {
          printf(&quot;argv[%u] = %s\n&quot;, i, argv[i]);
          if (argv[i][0] == '-')
          {
               if (argv[i][1] == 'i')
               {
                   caseIns = 1;
               }
               if (argv[i][1] == 'l')
               {
                   line = 1;
               }
               if (argv[i][1] == 'w')
               {
                   word = 1;
               }
               if (argv[i][1] == '-')
               {
                   fp1 = argv[i][2];
                   fp2 = argv[i][3];
               }
               else
               {
                   printf(&quot;Invalid option.&quot;);
                   return 2;
               }
          }
          else
          {
             fp1(argv[i]);
             fp2(argv[i][1]);
          }
      }
  
      /*
       * Check that files can be opened.
       */
      if(((fp1 = fopen(fp1, &quot;rb&quot;)) ==  NULL) || ((fp2 = fopen(fp2, &quot;rb&quot;)) == NULL))
      {
          perror(&quot;fopen()&quot;);
          return 3;
      }
      else
      {
          if (caseIns == 1)
          {
              if(line == 1 &amp;&amp; word == 1)
              {
                  printf(&quot;That is invalid.&quot;);
                  return 2;
              }
              if(line == 1 &amp;&amp; word == 0)
              {
                  if(compare_line(case_insens(fp1, fp2)) == 0)
                          return 0;
              }
              if(line == 0 &amp;&amp; word == 1)
              {
                  if(compare_word(case_insens(fp1, fp2)) == 0)
                      return 0;
              }
              else
              {
                  if(compare_char(case_insens(fp1,fp2)) == 0)
                      return 0;
              }
          }
          else
          {
              if(line == 1 &amp;&amp; word == 1)
              {
                  printf(&quot;That is invalid.&quot;);
                  return 2;
              }
              if(line == 1 &amp;&amp; word == 0)
              {
                  if(compare_line(fp1, fp2) == 0)
                      return 0;
              }
              if(line == 0 &amp;&amp; word == 1)
              {
                  if(compare_word(fp1, fp2) == 0)
                      return 0;
              }
              else
              {
                  if(compare_char(fp1, fp2) == 0)
                      return 0;
              }
          }
      }
      return 1;
  
      if(((fp1 = fclose(fp1)) == NULL) || (((fp2 = fclose(fp2)) == NULL)))
      {
          perror(&quot;fclose()&quot;);
          return 3;
      }
      else
      {
          fp1 = fclose(fp1);
          fp2 = fclose(fp2);
      }
  }
  
  /*
   * Function to compare two files line-by-line.
   */
  int compare_line(FILE *fp1, FILE *fp2)
  {
      /* Buffer variables to store the lines in the file */
      char buff1 [LINESIZE];
      char buff2 [LINESIZE];
  
      /* Check that neither is the end of file */
      while((!feof(fp1)) &amp;&amp; (!feof(fp2)))
      {
          /* Go through files line by line */
          fgets(buff1, LINESIZE, fp1);
          fgets(buff2, LINESIZE, fp2);
      }
  
      /* Compare files line by line */
      if(strcmp(buff1, buff2) == 0)
      {
          printf(&quot;Files are equal.\n&quot;);
          return 0;
      }
      printf(&quot;Files are not equal.\n&quot;);
      return 1;
  }
  
  /*
   * Function to compare two files word-by-word.
   */
  int compare_word(FILE *fp1, FILE *fp2)
  {
      /* File pointers */
      FILE *fp1, *fp2;
  
      /* Arrays to store words */
      char fp1words[LINESIZE];
      char fp2words[LINESIZE];
  
      if(strtok(fp1, &quot; &quot;) == NULL || strtok(fp2, &quot; &quot;) == NULL)
      {
          printf(&quot;File is empty. Cannot compare.\n&quot;);
          return 0;
      }
      else
      {
          fp1words = strtok(fp1, &quot; &quot;);
          fp2words = strtok(fp2, &quot; &quot;);
  
          if(fp1words == fp2words)
          {
              fputs(fp1words);
              fputs(fp2words);
              printf(&quot;Files are equal.\n&quot;);
              return 0;
          }
      }
      return 1;
  }
  
  /*
   * Function to compare two files character by character.
   */
  int compare_char(FILE *fp1,FILE *fp2)
  {
      /* Variables to store the characters from both files */
      int c;
      int d;
  
      /* Buffer variables to store chars */
      char buff1 [LINESIZE];
      char buff2 [LINESIZE];
  
      while(((c = fgetc(fp1))!= EOF) &amp;&amp; (((d = fgetc(fp2))!=EOF)))
      {
          if(c == d)
          {
              if((fscanf(fp1, &quot;%c&quot;, buff1)) == (fscanf(fp2, &quot;%c&quot;, buff2)))
              {
                  printf(&quot;Files have equivalent characters.\n&quot;);
                  return 1;
                  break;
              }
          }
  
      }
      return 0;
  }
  
  /*
   * Function to compare two files in a case-insensitive manner.
   */
  int case_insens(FILE *fp1, FILE *fp2, size_t n)
  {
      /* Pointers for files. */
      FILE *fp1, *fp2;
  
      /* Variable to go through files. */
      size_t i = 0;
  
      /* Arrays to store file information. */
      char fp1store[LINESIZE];
      char fp2store[LINESIZE];
  
      while(!feof(fp1) &amp;&amp; !feof(fp2))
      {
          for(i = 0; i &lt; n; i++)
          {
              fscanf(fp1, &quot;%s&quot;, fp1store);
              fscanf(fp2, &quot;%s&quot;, fp2store);
  
              fp1store = tolower(fp1store);
              fp2store = tolower(fp2store);
  
              return 1;
          }
      }
      return 0;
  }
  </code></pre>