4

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?

1
  • 1
    Which compiler you are using.. Which OS are you running this on? Commented Nov 2, 2012 at 9:38

4 Answers 4

5

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;
}
Sign up to request clarification or add additional context in comments.

Comments

1

argv is just a parameter to a function like anything else. Unless you make it global or pass it to "get_corners" it's not visible within that function.

To be honest you're probably better handling that file separately from the "get_corners" work anyway, it would appear to be cleaner. Try having a function to open and manage errors on the file read, then pass it's output to get_corners. Also you'll need to more carefully parse the command line than just casually passing it about. You could look at getopts in *NIX to help you, there are plenty of other libs around to make that task easier.

i.e.

FILE *readfile(filename)
{
    FILE *f = fopen(filename, "r");
    // Do some error checking
    return f;
}

main (int arc, char *argv[])
{
    FILE *myfile;
    myfile = readfile(argv[1]);
    get_corners(myfile, ...);
}

2 Comments

well, the problem is that the function definition has been determined by the instructor, and therefore cannot have argv. The same for global variable, we are not allowed to use global variable
Well my approach keeps the FILE pointer local and keeps your function signature the same. :-)
0

if you have write this line

*input = fopen(argv[1], "r");

under the get_corners() function so you have to know that argv is not a global variable. it's a variable seen by the main() function only

also you have to fix this line

*input = fopen(argv[1], "r");

by

input = fopen(argv[1], "r");

and put it directely under the main() function in order to see the argv parameter

Comments

0

If it's a requirement that get_corners() takes a FILE *, then you simply have to open the file in main(), where argv is available, and pass the resulting pointer to the function:

int main(int argc, char *argv[])
{
  if(argc > 1)
  {
    FILE *input = fopen(argv[1], "r");
    if(input != NULL)
    {
      const int corners = get_corners(input);
      fclose(input);
      /* Do something with corners here ... */
    }
  }

  return EXIT_SUCCESS;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.