Arguments for main in cmdLine - c++

g++ -pthread threads.cpp I'm compiling my program with that command and I don't know how should I specify some arguments given from the keyboard(file names).
int main(int argc,const char argv[]) is the head of my main and f1.txt and f2.txt are the files I want to pass.
./a.out f1.txt f2.txt is what I've typed and the answer was a screen full of unreadable characters.How should I do this?

int main(int argc,const char argv[]) is not correct, this int main(int argc, char** argv) is correct.
Then argv[1] and argv[2] will be your file names.

You should have got a warning from the g++, regarding the second argument in main.

Related

Using a program(.exe), inside a program, but passing command line arguments on linux C++

I've been trying to call a program inside another c++ program using the command "execvp()" from the <unistd.h> library but it gets core dump, and i dont know what i am doing wrong;
below its the code i use to call the program i want to use
#include <iostream>
#include <unistd.h>
int main(int argc, char **argv)
{
char *argument_list[]={argv[1],argv[2],NULL};
char *filename = "./bin/program.exe";
execvp(filename, argument_list);
return 0;
}
below this, its the code of the program i want to call
#include <iostream>
int main(int argc, char **argv)
{
int a = atoi(argv[1]);
int b = atoi(argv[2]);
std::cout << a+b<<std::endl;
return 0;
}
when i compile the the first code, i get a "main.exe" binary, then i type "main.exe 5 6" to sum both integers, and i get the "core dump" error.
Curious thing is, if i run gdb on it, i get the sum i want
the first command line, its running directly the "child" program, showing that it works. The second command line, its using the "main" program that calls the child one
(Obviously, this programs aren't the ones i need to apply this, they're just for illustration of the problem, they're really big codes, and it wouldn't be helpful to post them here);
How can i fix this?
So, it works if i set the first argument as the filename, as said by #WhozCraig, so now it works, and looks like this:
#include
#include <unistd.h>
int main(int argc, char **argv)
{
char *argument_list[]={"program.exe",argv[1],argv[2],NULL};
char *filename = "./bin/program.exe";
execvp(filename, argument_list);
return 0;
}
However, i get this warning:
1
How can i get around it? Is there a problem leaving it like this?

Non-portable const in main

Is using the main function in the form:
int main(int argc, const char* argv[])
non-portable and non-standard? The C++ standard says that argv is "pointer to pointer to char". I prefer to use the word const to prevent modification and out-of-bounds in an ​​unknown (to me) area of memory.
Is such a form wrong and non-portable?
Your version is indeed unportable.
The variants that the standard allows are
int main()
int main(int argc, char* argv[])
int main(int argc, char** argv)
The standard allows implementation-defined possibilities, so const might be permitted on some platforms. But if the implementation doesn't define such an alternative then the behaviour of your program is undefined.

Is it possible to pass input arguments to an executable in the command line

let's say I have this c++ code which I compiled into an executable out.exe
int main(){
int a = 0;
cin>>a;
if (a)
cout<<"done";
return 0;
}
Normally I would execute it using the command line by typing its name, then it would wait for my input.
However, what I want it to pass the input in the same line that I am calling the executable like so:
out.exe 1
1 being the input so the program wouldn't wait for my input and directly outputs:
done
You can use int main(int argc, char **argv) command line arguments **argv, and argument counter argc look in What does int argc, char *argv[] mean?.
The arguments are read as strings but you can easily convert them into the type you need, in this case int. How can I convert a std::string to int?

C++ How do I save argv to vector as strings

I am trying to save argv into a vector as strings but I keep getting the error: see reference to function template instantiation 'std::vector<_Ty>::vector<_TCHAR*[]>(_Iter,_Iter)' being compiled
I have tried Save argv to vector or string and it does not work
I am using Microsoft Visual Studio 2010.
Here is my code:
#include "stdafx.h"
#include <string>
#include <vector>
#include <iostream>
int _tmain(int argc, _TCHAR* argv[])
{
std::vector<std::string> allArgs(argv + 1, argv + argc);
return 0;
}
The problem is that std::string doesn't have a constructor for the _TCHAR* type, so you can't generate a vector of strings from an array of _TCHAR*.
Try using as said by #NathanOliver the "normal" version of the main: int main(int argc, char *argv[]).
Or switch to std::wstring.
Note: If not compiling with unicode enabled, _TCHAR* may then be equivalent to char * and the code would not provoke any compiler errors.
Use this:
typedef std::basic_string<TCHAR> tstring;
// Or:
// using tstring = std::basic_string<TCHAR>;
// If you have latest compiler
int _tmain(int argc, _TCHAR* argv[])
{
std::vector<tstring> allArgs(argv + 1, argv + argc);
return 0;
}
And if you would always want to use wide-strings, use this
int wmain(int argc, whar_t* argv[])
{
std::vector<std::wstring> allArgs(argv + 1, argv + argc);
return 0;
}
Or, if you want to have ANSI only (I'd not recommend), just use old style char and main.

C++ command line strings like Java?

Is there a way to get c++ strings from the commandline like in Java?
public static void main(String[] args)
where args is an array of C++ strings?
Not precisely but you can come close easily.
#include <iostream>
#include <vector>
#include <string>
using namespace std;
typedef vector<string> CommandLineStringArgs;
int main(int argc, char *argv[])
{
CommandLineStringArgs cmdlineStringArgs(&argv[0], &argv[0 + argc]);
for (int i = 0; i < cmdlineStringArgs.size(); ++i)
{
cout << cmdlineStringArgs[i] << endl;
}
return 0;
}
This just uses the overloaded constructor for std::vector that takes a begining/ending iterator pair to copy the command line arguments into the vectors. It is much the same as java from there on.
You can likewise build and object around that vector with utility methods to convert arguments but there is almost no point. Also there are plenty of packages with object that deal with interpreting command line switches and such. ACE, POCO, QT, etc.. all come with such facilities.
You can use a vector to get the char array into strings.
#include <vector>
#include <string>
using namespace std;
int main (int argc, char** argv)
{
vector <string> args (argv, argv + argc);
}
Yes the main function can take 2 arguments
int main(int argc, char* argv[])
Where argc is the number of arguments and argv contains a list of the arguments.
For example if you run your program as:
MyProgram.exe hello
Then
argc = 2
argv[0] = MyProgram.exe
argv[1] = "hello"
Not built in to the language, but its very easy to implement:
#include <vector>
#include <string>
using namespace std;
int main( int argc, char *argv[] ) {
vector <string> args;
for ( int i = 0; i < argc; i++ ) {
args.push_back( argv[i] );
}
// do something with args
}
In C, your main function signature looks like this:
int main(int argc, char** argv).
argc contains the number of arguments passed in by the command line. The name of the executable is in position 0, and adds one to argc.
argv contains an array of strings containing the arguments. Again, position 0 is the name of the executable.
The standard C++ main() signature is
int main(int argc, char* argv[])
where argc denotes the count of commandline arguments and argv[] is an array of primitive C strings holding the commandline arguments. argv[0] is the executable, as invoked from the commandline.
If you are writing a win32 application, you can use GetCommandLineW http://msdn.microsoft.com/en-us/library/ms683156(VS.85).aspx and CommandLineToArgW http://msdn.microsoft.com/en-us/library/bb776391(VS.85).aspx
There is a comment on the CommandLineToArg page about special handling that is needed if your executable has spaces in the path and there are no arguments that you might have to handle.