To fix an error that @claptrap suggests, replace:
file=(char *) malloc(2);
strcpy(file,optarg);
with safer:
file = strdup(optarg);
It will allocate and duplicate the string for you automatically, whatever length it has. You have the strdup defined in string.h which you already have included.
After you use the file string, you should free it from memory using:
free(file);
Strdup manpage. Also check out strncpy function which is safer to use than strcpy, because it knows how many character it can copy into target buffer before overflowing it.