parsing input and output filename as argument from command line - c++

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

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...
}

Related

Passing C++ command line arguments inside main(), yet do the same job

Is there a way to keep the command line argument as standard and yet pass the argv internally inside the main?
Like, change this:
int main(int argc, char **argv) {
App app(argc,argv);
return app.exec();
}
to something like this:
int main(int argc, char **argv) {
vector<string> argv ="arguments";
int argc = X;
App app(argc,argv);
return app.exec();
}
When I have:
./a.out -a A -b B -c C
I know this is weird. I just want to find a workaround not to change the source code of a huge project and just run my command line arguments every time with only ./a.out.
Put each of your arguments in a char array, and then put pointers to those arrays into an array of pointers.
char arg1[] = "./a.out";
...
char argN[] = "whatever";
char* argv[] = { arg1, ..., argN}
App app(N, argv);
You may be looking for
const char *myArgv[]={"-a","A","-b","B"};
int myArgc=4;
App app(myArgc,myArgv);
return app.exec();
std::vector<std::string> args(argv, argv+argc);
You may rename arguments passed to app as you wish to avoid a name conflict, for ex.
int main(int argc, char **argv) {
vector<string> app_argv = /* contents */;
int app_argc = app_argv.size() ;
App app(app_argc, app_argv);
return app.exec();
}

Command line parameters c++ [duplicate]

How do I read command-line parameters in C? For example, in
./test --help
or
./test --build
how do I access "--build" or "--help"?
Your parameters are in argv:
int main(int argc, char **argv)
if you printf the content of argv (argv[0],argv[1] etc) youll get the idea.
try:
int main (int argc, char **argv)
{
for(int i = 0;i< argc;i++)
printf("%s\r\n",argv[i]);
}
You can use the argc and argv arguments to the main function and do different things based on them:
#include <string.h>
void dohelp(void) { /* whatever; */ }
void dobuild(void) { /* whatever; */ }
int main(int argc, char **argv) {
if (argc == 2) {
if (!strcmp(argv[1], "--help")) dohelp();
if (!strcmp(argv[1], "--build")) dobuild();
}
return 0;
}
argc contains the number of parameters passed by the shell to your program, including the program name. So myapp --help gets an argc of 2.
argv are the arguments themselves. The last argv (argv[argc]) is the NULL pointer.
Edit: the parameters don't need to be named argc and argv, but naming else something else is very, very bad!
int main(int foo, char **bar) /* RGAGGGGHH */
int main(int n, char **options) /* RGAGGGGHH */
The very basic is to use the arguments (int argc, char *argv[]) and you can parse those directly.
One more advanced method is to use getopt... http://www.gnu.org/s/libc/manual/html_node/Getopt.html
Command line arguments are arguments passed to your program with its name. For example, the UNIX program cp (copies two files) has the following command line arguments:
cp SOURCE DEST
You can access the command line arguments with argc and argv:
int main(int argc, char *argv[])
{
return 0;
}
argc is the number of arguments, including the program name, and argv is the array of strings containing the arguments. argv[0] is the program name, and argv[argc] is guaranteed to be a NULL pointer.
So the cp program can be implemented as such:
int main(int argc, char *argv[])
{
char *src = argv[1];
char *dest = argv[2];
cpy(dest, src);
}
They do not have to be named argc and argv; they can have any name you want, though traditionally they are called that.
There are several ways to do it [as usual].
Command line arguments are read from argv (passed to main along with argc).
You can parse those yourself and have a bit switch setting flags each time a new option is found in argv. Or you can use a library to parser command line arguments. I suggest libc getopt (google it).

c++ default argv if no parameter is parsed in console

i would like to give my file a default argv, if none is given in the console. It doesn't work cause of duplicate parameter names.
if no parameter is given, i would like to use a fixed filename in the same folder.
int main(int argc, char* argv[], char* argv[1]="test.ps1")
{
std::string target = _T(argv[1]);
std::string temp= std::string("powershell.exe -command \"")+target +std::string("\"");
ShellExecuteA(0, "runas", "powershell.exe", temp.c_str, "", SW_HIDE);
}
int main(int argc, char **argv) {
std::string const default_file = "test.ps1";
std::string file = (argc < 2) ? default_file : argv[1];
doSomethingWithFile(file);
}

Open file name passed from commandline argument that is in different location in the server

I want to open the filename name that I sent from command line but the file is in /home/docs/cs230 . Below is the code I tried but it shows error when I tried to compile in linux:
int main(int arg, char* args[1]) {
// Open the file
newfile = fopen("/home/docs/cs230/"+args[1], "w+b");
}
Since this is C++ we can use std::string like so:
int main(int arg, char* args[]) {
// Open the file
std::string path( "/home/docs/cs230/" ) ;
path+= args[1] ;
std::cout << path << std::endl ;
FILE *newfile = fopen( path.c_str(), "w+b");
}
Mats also makes a great comment that in C++ we would use fstream, which you can read more about at the link.
Since this is C++, I would suggest this:
int main(int argc, char *argv[])
// Please don't make up your own names for argc/argv, it just confuses people!
{
std::string filename = "/home/docs/cs230/";
filename += argv[1];
newfile = fopen(filename.c_str(), "w+b");
}
[Although to make it fully C++, you should use fstream, not a FILE
If you want to stick with pointers you can concat the string (char*)
const char* path = "/home/docs/cs230/";
int size1 = sizeof(argv[1]);
int size2 = sizeof(path);
const char* result = new char[size1 + size2 + 2];
result[size1 + size2 + 1] = '\0';
memcpy( result, path, size1 );
memcpy( &result[ size1 ], argv[1], size2 );
not a recommended option, but there are a bunch of possibilities here.

How to get 1st parameter in main() in C++? [duplicate]

This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
Pass arguments into C program from command line.
mypro parameter
When run like above,how to get the parameter in mypro's main() :
#include <iostream>
int main()
{
char* str = "default_parameter";
if(parameter_exists())str = parameter;
...
}
How to implement the pseudo code above?
Just need to add (int argc, char *argv[]) to your main function. argc holds the number of arguments, and argv the arguments themselves.
int main(int argc, char *argv[])
{
std::string str = "default";
if (argc > 1) { str = argv[1]; }
}
Note that the command is also included as an argument (e.g. the executable). Therefore the first argument is actually argv[1].
When expecting command-line arguments, main() accepts two arguments: argc for the number of arguments and argv for the actual argument values. Note that argv[0] will always be the name of the program.
Consider a program invoked as follows: ./prog hello world:
argc = 3
argv[0] = ./prog
argv[1] = hello
argv[2] = world
Below is a small program that mirrors the pseudocode:
#include <iostream>
#include <string>
int main(int argc, char **argv) {
std::string arg = "default";
if (argc >= 2) {
default = argv[1]
}
return 0;
}
Your function should use the second of these two allowed C++ main function signatures:
int main(void)
int main(int argc, char *argv[])
In this, argc is the argument count and argv is an argument vector.
For more details, see this Wikipedia article.
Well you need a main function that accepts arguments, like so:
int main(int argc, char *argv[])
{
}
You would then check the number of arguments passed in using argc. Usually there is always one, the name of the executable but I think this is OS dependent and could thus vary.
You need to change the main to be:
int main(int argc, char** argv){
argc is the number of paramaters, argv is an array containing all of the paramaters (with index 0 being the name of the program). Your first parameter will be located at index 1 of argv.
#include <iostream>
using namespace std;
int main(int argc, char* argv[]) {
for(int i = 1; i < argc; i++)
cout << atoi(argv[i]) << endl;
return 0;
}
Here argc gives the count of the arguments passed
and argv[i] gives ith command line parameters.
where argv[0]='name of the program itself'
main has two parameters, int argc and char** argv which you can use to access to the command line parameters.
argc is the number of parameters including the name of the executable, argv points to the parameter list itself (so that argv[0] is "mypro" in your example).
Just declare main like
int main (int argc, char** argv){..}
You need to use this declaration of main:
int main(int argc, _TCHAR* argv[])
{
if (argc > 1)
{
str = argv[1];
}
}
argv[0] is the name of the executable, so the parameters start at argv[1]
On Windows, use the following signature instead to get Unicode arguments:
int wmain(int argc, wchar_t** argv)