1

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

3
  • 1
    In C++ you would do nearly the same as in C#, that is: 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(). Commented Nov 23, 2016 at 9:57
  • You are not able to compile it, because...? Commented Nov 23, 2016 at 9:57
  • file_process::process will need a type of parameter, not just a name. Don't forget arg[0] is the exe name. Don't forget const where needed. Or use a std::string Commented Nov 23, 2016 at 10:05

1 Answer 1

1

Here is one way to do it:

int main(int argc, char** argv)
{
    assert(argc >= 3);
    const std::string iFilename = argv[1];
    const std::string oFilename = argv[2];
    file_process::process(iFilename, oFilename);
}

And you file_process::process could be:

void file_process::process(const std::string& iFilename, const std::string& oFilename)
{
    body...
}
Sign up to request clarification or add additional context in comments.

7 Comments

I did as you said But when I am editing my file_process::process as void file_process::process(const std::char& iFilename[], const std::string& oFilename[]); the compiler shows an error in the above line as " error: expected ‘)’ " , though I can't see any need for this closing paranthesis. Another error in the above line is "expected initializer" I initialized the iFilename and oFilename as global const as: const char iFilename[100]; const char oFilename[100];
Either go for C-strings or C++-strings you should not mixed, that is just weird. And it is not std::string[] that is an array of std::string...
Thank you for pointing that out it was a typo. I fixed the code now, your second point regarding the array of std::string was helpful indeed. Now I am having the problem at runtime stating "int main(int, char**): Assertion 'argc>=3' failed."
The assert is there to ensure you remember to actually provide command-line arguments, if you don't it will assert.
I have another problem in the project now. I am using netbeans and I didn't add arguments in the project properties. Now the project is compiling but at the run time it states /bin/sh: 1: Syntax error: Unterminated quoted string I googled but couldn't find a concrete answer. Any idea what is the problem ?
|

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.