Phorce Phorce - 1 year ago 83
C++ Question

Passing file location through main?

I am building a web application / Interface for my C++ program which will be hosted on the server and then using a scripting language ("PHP") I will then execute the program to run.

I am using G++ to compile the code and and I execute the command to run like so: ("./main") now is it possible that I can pass in the file location so then my program can run? So for example like this:

int main(int argc, char *argv[], string* fileLoc)
// code


Then execute like this ("./main(FILE_LOCATION)")?

Hope someone can help

Answer Source

You should keep the standard main signature int main(int argc, char *argv[]). The filename would be in argv[1], provided you execute it like this:

./main somefilename.txt

#include <iostream>
int main(int argc, char* argv[])
  if (argc > 1)
    std::cout << argv[1] << "\n";

./main Hello_there


Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download