I don't really know how to ask this...
Supposed I want to read a file from my function, but I have no idea what will be the filename that I want to read because the filename will be passed in my main function as command line argument (argv[])
so my main looks like :
int main(int argc, char *argv[])
my function will look like:
int get_corners(FILE *input, int size, and so on)
What I've tried in my function:
*input = fopen(argv[1], "r");
but, compiler said it doesn't recognize variable argv
So, can someone please help me understand how to call input file when you are not in main and have to deal with command line parameter?
First off you should not dereference a FILE*, the structure is opaque. That means that you just pass pointers around.
Secondly what you want is probably to pass the file name that you have got from the command line to your function as a function parameter. In C variables are not inherited from other function's scope's, either at runtime or at compile time.
Something like this
int main(int argc, char* argv[])
{
if (argc < 2)
return 1;
printf("%d corners\n", getCorners(argv[1]));
}
int getCorners(char* file) {
FILE* input = fopen(file, "r");
.. do soemthing interesting ...
return cornerCount;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With