Passing argv and argc to main as an array - c++

I am using opencv, and have a function that takes command line variables:
int start(int argc, char** argv)
{
cv::CommandLineParser parser(argc, argv, keys);
if(parser.has("help")) {
parser.printMessage();
return 0;
}
....
}
what I would like to do, is have another function that passes variables to this one, instead of using the command line, as it is now.
the command line I pass it to run perfectly is:
-h=6 -w=8 -pf=defaultConfig.xml -t=charuco -v=bmCalib2.mov -sz=0.045 -d=1
So I have this function:
void main()
{
char* passArg[7] = { "-h=6", "-w=8","-pf=defaultConfig.xml", "-t=charuco","-v=bmCalib2.mov", "-sz=0.045", " -d=1" };
start(7,passArg);
}
It compiles and runs, but the results are different (it is a calibration application, the command line calibrates, and the function version does not).
Is this the correct way to send int argc, char** argv instead of using command line?
thank you.

Ah, already solved. I needed to adjust my char array to:
char* passArg[8] = { NULL,"-h=6", "-w=8","-pf=defaultConfig.xml", "-t=charuco","-v=bmCalib2.mov", "-sz=0.045", " -d=3" };
Obviously the .exe is usually the first variable given in a command line launch.

Related

How to use parameters from commandline inside a main function in C++ using SET-Command

Sorry, I have never really worked with Commandline parameters. Maybe this is a stupid question:
I have a simple file test.cpp with a main function. I now want to start this program from the Commandline after having set the variable var with a value from the Commandline. (How) Can I do this?
test.cpp:
int main(int argc, char *argv[])
{
int var; // do I need to declare this variable here?
if (var == 123)
puts(" var=123 !!");
}
In the Commandline can I type something like that:
set /A var=123
test
And main would print "var=123 !!"
set defines an environment variable. That's a variable that exists in the environment of your application, i.e. outside your application. You can't use that to set variables inside your program.
You can however access the environment using std::getenv.
#include <iostream>
#include <cstdlib>
int main(void) {
const char* env_abc = std::getenv("ABC");
if (env_abc)
{
int abc = std::atoi(env_abc);
std::cout << abc;
}
}
If you mean running from command line something like "test 123",
then the argv[] argument contains your command line, when argv[0] is the name of the process, and argv[1] and so on are your command line arguments represented as strings
int main(int argc, char *argv[])
{
int var = atoi(argv[1]);
if (var == 123)
puts(" var=123 !!");
}

How to insert parameters to command prompt when starting a program in c++?

I have no idea how to work with command prompt. I don't even know which parameters I should write. Here's the code we were given:
//random example function
void KMP(const string& text, const string& sample) {
ofstream file("iz.txt");
file<<"Test: "<<sample;
file.close();
}
int main(int argc, const char *const argv[]) {
if (argc != 3) {
return -1;
}
string text = inputText(argv[2]);
string sample = argv[1];
out.open("out.txt");
if (!out) {
return -2;
}
KMP(text, sample); //KMP algorithm function that I finished coding in my program (it would be too long to copy-paste all here), but I don't know how to call it in command prompt.
return 0;
}
How can I start the program with command prompt
The passing of command line arguments is not specified by the C++ language. It is specified by the shell of the operating system that you are using. Quite commonly, program's execution follows this pattern:
./path/to/executable argument1 argument2
First, you need to compile the program.
In command prompt-
Go to the respective path. Now,
g++ /yourfilename.cpp/ -o main
Now to run the file,
./main parameter1 parameter2

project->peoperties->debugging->Command Arguments does't work in VS community 2015

I see the command line argument (coreProgram.txt) in the properties pages but the it is not recognized in the program i.e.
int main (int argc, char* argv[])
does not recognize argv[1] is in fact argc = 1

openCV load_object_detect function

I am trying to run a face detection code in openCV and I don't want to use the command prompt to run it but I don't know what to give as an input argument to load_object_detect.
here is the sample from the code:
CvHaarClassifierCascade* cascade = load_object_detector(argv[2]);
In order for the above command to execute the code should be run from the command prompt which I am trying to avoid for now...
All I know is that its input type is const char* cascade_path...
try argv[2] = whatever path you want to put;
just before calling the function
Something like this
#include<iostream>
using namespace std;
int main(int argc , char *argv[])
{
argv[2] = "SuvP";
std::cout<<"Hey "<<argv[2]<<endl;
return 0;
}
The output is Hey SuvP
The arguments from command line are stored in the array argv. Alternatively we are filling data in the array within the code and not from the command line in this case.

Read values using `backticks` instead of Pipe | C++

on shell there is the possibility to give a output to another programm using a Pipe.
for example :::
ps axu | grep someprocess
Now i want to programme a C++ Programme that accepts those Pipes too.
I found a solution like.
using namespace std;
int main()
{
string mypipe;
if(cin);
{
cin >> mypipe;
cout << mypipe << endl;
}
return 0;
}
Now i want, that i am able to call my function with using backticks.
for example i am using a shell construct like this.
./myprog.bin `./otherprog.bin someparameter`
How can i read the output that otherprog.bin generates into my Programm using Parameters instead?
Add commandline parameters to your main function, like this:
int main( int argc, const char* argv[] )
argc is the argument count, argv the table that holds them.