Soulreaper_10 February 2016

reading and writing files using flags in command line in c

My program needs to parse command line arguments and then read contents of a file and/or write those contents to another. The user must enter the commands using flags,where i stands for reading a file and o for writing, like this:

./program -isample -ofile

Errors:

warning: passing argument 1 of ‘fgets’ from incompatible pointer type
warning: passing argument 1 of ‘fputc’ makes integer from pointer without a cast

Any help will be much appreciated!

#include <stdio.h>
#include <unistd.h>

int main (int argc, char *argv[]) 
 {
   int opt = 0;
   FILE *file = NULL;
   FILE *text = NULL;
   char ch[200];

   while ((opt = getopt(argc, argv, "i:o:")) != -1) 
    {
      switch(opt) 
       {
          case 'i':
           {
              file = fopen(argv[1], "r");
              while(fgets(ch, 200, file) != EOF)
              {
                printf("%s\n",ch);
              }
              fclose(file);
           }
           break;
          case 'o':
           {
             file = fopen(argv[1], "r");
             text = fopen(argv[2], "w");
             do
              {
                 ch = fgets(file);
                 fputc(ch, text);
              }while (ch != EOF);
             fclose(file);
             fclose(text);
           }
           break;
          case '?':
          {
            if (optopt == 'i') 
             {
               printf("\nMissing mandatory input option");
             } 
            else if (optopt == 'o') 
             {
               printf("\nMissing mandatory output option");
             }
            else 
             {
               printf("\nInvalid option received");
             } 
          }
           break;
       }
    }

 printf("\n");
 return 0;
}

Answers


chqrlie February 2016

Your program has several problems:

  • you should parsing all arguments before copying: there might be a -o option after the -i option.
  • The filename should be optarg
  • fgets returns NULL at end of file
  • printf will duplicate the \n, you should use fputs.
  • fputc does not take a buffer
  • fgets takes 3 arguments.

Here is a corrected version:

#include <stdio.h>
#include <unistd.h>

int main(int argc, char *argv[]) {
    char *infile = NULL;
    char *outfile = NULL;
    FILE *file = stdin;
    FILE *text = stdout;
    char ch[200];

    while ((opt = getopt(argc, argv, "i:o:")) != -1) {
        switch (opt) {
          case 'i':
            infile = optarg;
            break;
          case 'o':
            outfile = optarg;
            break;
          case '?':
            if (optopt == 'i') {
                fprintf(stderr, "Missing mandatory input option\n");
            } else
            if (optopt == 'o') {
                fprintf(stderr, "Missing mandatory output option\n");
            } else {
                fprintf(stderr, "Invalid option received\n");
            } 
            break;
        }
    }
    if (infile) {
        file = fopen(infile, "r");
        if (file == NULL) {
            fprintf(stderr, "cannot open input file %s: %s\n", 
                infile, strerror(errno));
            exit(1);
        }
    }
    if (outfile) {
        text = fopen(outfile, "r");
        if (text == NULL) {
            fprintf(stderr, "cannot open output file %s: %s\n",
                outfile, strerror(errno));
            exit(1);
        }
    }
    while (fgets(ch, 200, file) != NULL) {
        fputs(ch, text);
    }
    fclose(file);
    fclose(text);
    return 0;
}

Post Status

Asked in February 2016
Viewed 3,739 times
Voted 14
Answered 1 times

Search




Leave an answer