I am new with c++ and in a project i need to use command line argument. I read about command line arguments i.e including
int main(int argc, char** argv)
{
}
but i have a problem declaring my filename in the source file.
i declared my input and output filename in the source file(file_process.cpp) as
const char iFilename[] ;
const char oFilename[] ;
defined the function(which uses the input file - iFilename and process the output in the oFilename) as
void file_process::process(iFilename[], oFilename[])
{
body...
}
and in the main method as :
int main(int argc, char** argv) {
iFilename[] = argv[1];
oFilename[] = argv[2];
file_process::process(iFilename[], oFilename[]);
}
earlier i hardcoded the filename to test my program without arguments in the main method and declaring the variable in the source file(file_process.cpp)as:
const char iFilename[] = "input_file.pdf";
const char oFilename[] = "output_file.txt";
and its working fine but when I am trying to take the arguments from the command line as stated above and I am not able to compile it.
Is it the right way of doing it in c++ ? I work with c# and there simply declaring in the source file like:
string iFilename = args[0];
string oFilename = args[1];
works. I
std::string iFilename = argv[1], and then pass the variable as a C++-string to the function or as a C-string using std::string::c_str().file_process::processwill need a type of parameter, not just a name. Don't forget arg[0] is the exe name. Don't forgetconstwhere needed. Or use astd::string