Passing filename to a function within a class

Hi,
I was hoping someone could help me out with this problem, C++ is a bit new to me.
Basically, I want to have a program which takes in a filename in the main function and passes it to a function within a class. Here's my attempt so far, which as you can guess isn't working.

class Parse
{
public:
Parse(); // default constructor
~Parse(); // deconstructor
void File(char* argv[]); //opens and reads in file
private:
};

void Parse::File(char* argv[]){
ifstream FileIn;
vector<string> tokens;

FileIn.open(argv[1],ios::in);
//....etc
}

int main(int argc, char **argv)
{
Parse Par;
Par.File(argv[1]);

return EXIT_SUCCESS;
}

I hope that's clear, any tips and explanations would be much appreciated.
Michael
Don't pass a char *[], pass a char *, and then simply pass that to open(). And use a more descriptive name than argv, like filename, or something.
Last edited on
Thanks helios, but could you elaborate a little on your reply.
I agree I should use a more descriptive name than argv, but can you explain the first part of your answer a bit more.
It's simple.
argv, as declared in main(), is a char **, a pointer to an array of pointers to arrays of chars. Each element of argv should therefore be a char *, a pointer to an array of chars.

If you want to just pass argv, remove the "[1]" from your call to Parse::File().
Keep in mind that char *var[] is the same as char **var

The reason for this is that behind the scenes, when a parameter type is param[], it is interpreted as *param in c++. Similarly, *(param[]) is equivalent to *(*param), or just **param.

This is why you can use either:

 
int main(int argc, char** argv) {}


Or:

 
int main(int argc, char* argv[]) {}

Thanks, got it working
Topic archived. No new replies allowed.