i cant see that well. i have a eye condition so bear with me if i am wrong. but it seems like you are opening the same file using two different functions at the same time and the error message cant open file -n means that your are not parsing the options correct.
i posted a program i made just for the example of how i would have organized the program. this program opens the files and does something depending on the flags /options giving correct. (i tested it two times on each option but it could have small flaws ,i am still newbie. )
Code:
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
void PrintUsage(const char *progname);
int main (int argc, char **argv)
{
int nflg = 0;
int sflg = 0;
int vflg = 0;
int tflg= 0;
int index;
int c;
opterr = 0;
FILE *fp;
/* if the program has no arguments at all then print usage*/
if(argc == 1)
{
PrintUsage(argv[0]);
exit(0);
}
/* parse the options */
while ((c = getopt (argc, argv, "nsvt")) != -1)
switch (c)
{
case 'n': nflg = 1; break;
case 's': sflg = 1; break;
case 'v': vflg = 1; break;
case 't': tflg = 1; break;
case '?':
if (isprint (optopt))
{
fprintf (stderr, "Unknown option `-%c'.\n", optopt);
}
else
{
fprintf (stderr,
"Unknown option character `\\x%x'.\n",
optopt);
exit(EXIT_FAILURE);
}
default:
abort ();
}
/* the version flag triggered here */
if(vflg != 0)
{
printf("version is beta version 1 \n");
exit(EXIT_SUCCESS);
}
/* the things that happens in the program here*/
for (index = optind; index < argc; index++)
{
if((fp=fopen(argv[index],"r")) == NULL)
{
printf("error in opening file %s",argv[index]);
}
while((c=getc(fp)) != EOF)
{
/* do some stuff here with the file */
if(nflg)
{
if(c == '\n')
printf("%c<br>",c);
else
printf("%d",c);
}
if(sflg)
{
printf("%c\t%d",c,c);
}
if(tflg)
{
printf("%0xhh",c);
}else
printf("%c",c);
}
fclose(fp);
}
return 0;
}
void PrintUsage(const char *progname)
{
fprintf(stdout,"USAGE:%s [options]\n"
"barebones usage help text\n",
progname);
}