This is a homework assignment first off.
We have to create a “common application-programming model in UNIX/Linux known as the filter”.
I’m stuck on reading the input passed through as arguments (it’s all I ever seem to have trouble on).
For example, the cmd is open and the following line is entered:
program -isomebinaryfile.bin
I need to determine what the first letter is after the hyphen (-) and so on and so forth.
Is scanf what I would be using? My main is set up to be able to accept arguments:
int main (int argc, char *argv[])
{
FILE *inf = NULL;
char *arg = argv[0];
}
Can someone give me a little help?
Unless your assignment is only to handle processing of arguments, you may want to look up
getopt– it’s a standard library parser for arguments.As for the meat of your question, there are a lot of options, and you could use
sscanfas part of it, but you don’t have to.To parse the one argument you mentioned, you need to do the following: check if the argument begins with -i, grab the data out of the argument.
The easiest way to check if the argument begins with -i is:
Alternatively, if you have a lot of argument options, all beginning with ‘-‘, you may want something like:
Note, I’m using argv[1], not 0. argv[0] is always the name of the executable.
The fastest way to extract the rest of the argument is simple pointer arithmetic:
This is most efficient – it reuses the strings that are already in argv. If you’re planning on changing the strings around, you’d do better with strcpy:
Play around and experiment with all the string functions. You’ll find them essential to all of your later programs.