Reputation: 19
I am trying to make it so that you would call the method with the function to perform for example -r,-u,-l and then a file name or work with standard input how would I make it so that it can take 3 inputs I've tried a couple things but I just started using c and have almost no clue what I am doing.What I can't figure out is how to make an input take three then be able to compare the strings to choose what operation to do.
#include <stdlib.h>
void upper(FILE *src, FILE *dest)
{
int c;
{
fprintf(dest, "%c", toupper(c));
}
}
void lower(FILE *src, FILE *dest)
{
int c;
while ((c = fgetc(src)) != EOF)
{
fprintf(dest, "%c", tolower(c));
}
}
void rot13(FILE *src, FILE *dest)
{
int c;
while ((c = fgetc(src)) != EOF)
{
fprintf(dest, "%c", c+13);
}
}
FILE * input_from_args(int argc,char choice, const char *argv[])
{
if (argc == 1)
{
return stdin;
} else
{
return fopen(argv[1], "r");
}
}
FILE * input_from_args(int argc, const char *argv[])
{
if (argc == 1)
{
return stdin;
} else
{
return fopen(argv[1], "r");
}
}
int main(int argc,char** choice,const char *argv[])
{
FILE *src = input_from_args(argc, argv);
FILE *dest = stdout;
if (src == NULL)
{
fprintf(stderr, "%s: unable to open %s\n", argv[0], argv[1]);
exit(EXIT_FAILURE);
}
else if(*choice == '-r')
}
rot13(src,dest)
}
else if(*choice == '-u')
{
upper(src,dest)
}
else if(*choice == '-l')
{
lower(src,dest)
}
fclose(src);
return EXIT_SUCCESS;
}
Upvotes: 0
Views: 91
Reputation: 409364
That's what the argv
array is for. It will contain all arguments passed to the program.
For example take this simple test program:
#include <stdio.h>
int main(int argc, char *argv[])
{
printf("argc = %d\n", argc);
for (int a = 0; a < argc; ++a)
{
printf("argv[%d] = \"%s\"\n", a, argv[a]);
}
}
If you build that source, and execute the program such as
$ ./a.out argument1 argument2 argument3
It will output
argc = 4 argv[0] = "./a.out" argv[1] = "argument1" argv[2] = "argument2" argv[3] = "argument3"
In other words, the arguments passed to a program does not match the arguments in the source code, instead they are converted to the argv
array.
Upvotes: 3