Using Command line help

I am having an issue with the statement "Both the input and output files' names should be read from the command line." I don't understand what this means or what I need to do. Thank you for the help.
You need to grab the names of your targets off of the command line. The first argument to main is an integer telling your application how many entries are available to be read off of the command line. The second one is a zero-indexed array of char pointers to c-strings that were passed to your application from the shell. The first on of these is usually the fully qualified path to the binary, any thing after that is\are space delimited entries on the command line. You would treat these in main basically the same way you would treat any other arguments to a function.
Thank you for your reply. However, I am still not clear on what you mean. Is there an example you can give me that might make it clear to me? Thank you again.
1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
#include <string>

int main(int NumShellArgs, char* Arguments[])
{
      for(int i = 0; i < NumShellArgs, ++i)
      {
            std::cout << Arguments[i] << "\n";
      }

      return 0;
}


Compile the code above for a visual aid, start your program from the command line and feed it strings to echo or use your IDE to pass arguments in.

You can treat the variables passed from the shell just like any other argument to a function, so if you want to copy them, then just do it:
1
2
std::string strArgument = Arguments[0];
std::cout << strArgument << "\n" << Arguments[0] << "\n";

These will be the same. Here the constructor for the type std::string takes the char pointer, casts it to a const char pointer implicitly, and makes your std::string object. As mentioned before, this one should be the fully qualified path and name of your compiled executable.
If you use Windows, press the windows+r keys, and type "cmd". Or select start->run and type "cmd". That will bring up a console window where you can type commands on the command line. in your example, you type the name of the program (including full or relative path if necessary) followed by whatever parameters the program expects, separated by spaces.

Last edited on
Topic archived. No new replies allowed.