Get parameter from exe in C++ - c++

As we know the Query String in web. It's key/value go with the website URL ex: abc.com?myName=stack
For example in PHP, if we want get value of the myName, just do this $_GET['myName']
So, in C++, how can I get it?
in C# I pass an parametter to an *.exe file ( this exe file is C++ code ).
In C++ code, how to get this parametter value .

Build a console application with just the following code:
#include <iostream>
int main(int argc, char** argv)
{
for(int i = 1; i != argc; ++i )
{
std::cout << argv[i] << std::endl;
}
}
Assuming the name of the .exe is mytest.exe, execute it with some arguments, such as:
mytest.exe Hello there.
You should get the following output:
Hello
there.
Hope the simple example makes it clear as to how to process command line arguments in C++.

Have no idea about your situation, but surely you will have realize what parameters do you really need?
If you just need arguments from the command line, simple use the char** argv variable. In complicated cases you can use GNU getopt or even Boost::Program_options (the last is cross-platform);
If you are trying to access environmental variables, use standard getenv functions.

Related

VideoCapture with " int main(int argc, char **argv) "

I'm trying to use VideoCapture. a part of my code is below.
when I run my code, I got this :
Error! Insufficient parameters provided.
How can I use my video with this code. I want to open a stream with VLC.
Or if is there any other way, I'd like to use.
I searched that argv[1] is will be my video file. But I don't know how to show my file and how to define my file to this code.
To help future users, I'd make some changes:
Was:
LOG_DEBUG("Error! Insufficient parameters provided.");
Is:
std::string program(argv[0]);
LOG_DEBUG("Error! Insufficient parameters provided.");
LOG_DEBUG("Please provide a command line argument.");
LOG_DEBUG("Example: " << program << " VIDEO_FILE_NAME");
Explanation:
On the command line, when the program is invoked, the arguments from the command line are copied in the array of strings held by argv. argv[0] is the first argument and it is the filename of the program itself. Put another way, argv[i] for 0 <= i < argc are populated in the array of strings argv from the command line. If you renamed the program executable file, argv[0] would be different the next time you ran the program.
The array argv is indexed from 0 to argc-1. When main is invoked this array of strings and argc are set. It's up to the software to decide what to do. In this case your application tests argc and finds that if no argument is provided (ie., argc < 2) then the one user argument provided by the user is not present, report the error and return.
Incidentally, there's yet another form of main you can use:
int main(int argc, char** argv, char** envp)
argc = number of arguments.
argv = array of argument strings
envp = array of environment variable name=value pairs
So beyond simple command line argument passing, one could choose to write the main function to grab the environment variables (not shell variables) and decide nuanced action based on that. Options abound.
But for the time being, your code would be helpful if it reported why there is an error and the suggestions provided seem to do that.
Good luck.

Inputting values from .mat file to array in C++

I am trying to copy a 1100x1100 matrix from a .mat file to an array variable of type float in C++. I read online and found that the matio library is a good option. I installed their library using "make" on Ubuntu 12.04 (I followed the method given on their webpage).
However, I am unable to write code using it mainly because I am new to C++. I am using g++ to compile the file. I get errors such as "unknown reference to Mat_Open" and so on.
I did find this bit of code on the webpage:
#include <stdlib.h>
#include <stdio.h>
#include "matio.h"
int main(int argc,char **argv)
{
mat_t *matfp;
matvar_t *matvar;
matfp = Mat_Open(argv[1],MAT_ACC_RDONLY); //here argv[1] is "a.mat"?
if ( NULL == matfp )
{
fprintf(stderr,"Error opening MAT file %s0,argv[1]);
return EXIT_FAILURE;
}
matvar = Mat_VarReadInfo(matfp,"x"); // x is the variable we are trying to access?
if ( NULL == matvar )
{
fprintf(stderr,"Variable ’x’ not found, or error reading MAT file\n");
}
I have a couple of questions:
here, argv[1] corresponds to the .mat file I am trying to open right?
x in this code is the variable present in the .mat file I am trying to copy?
When I ran this code, I received errors stating - Unknown reference to Mat_Open and so on. Another couple of the same type of errors also were there.
I compiled this using : g++ abc.cpp -o test. (Followed by ./test. But I never got around to that due to the errors obtained during compilation).
How can I make it work? Is there any mistake with the code I used? Or with the compile statement I am using-maybe there are some linkers I need to use for compilation.
Thank you. Please remember that I am new to C++. Any advice would be helpful.
1) argv[1] - is a first parameter you put after your program call. If you want to "feel it", use debugger or code like this:
#include <iostream>
for (unsigned i = 0; i < argc; ++i)
{
std::cout << argv[i] << std::endl;
}
2) Yes, looking at http://libmatio.sourcearchive.com/documentation/1.3.4/group__MAT_g4c8205ff25c5b688a40775fbb1840b7e.html I can say, that you will read variable with name "x".
3) "undefined reference" means you need to build with matio libraries. Add something like "-lLibraryName" to your compile string. And it will have to be built.
To avoid many problems, try to install Code::Blocks, it's cross-platform and quite easy to start using C++ if you never did it before. It also supports debuggers, so you will avoid many problems quite easy.

Call a C++ project main() in Python in Visual Studio?

I have a C++ project and a Python project under one solution in Visual Studio. I am reluctant to modify the C++ project, because it is complicated and now complete. I don't want to touch on it any more. So to integrate them, I choose to call the C++ project in Python, instead of the other way round.
I wish to pass the parameters from Python to
int main(int argc, char** argv)
of the C++ project.
How may I do it?
The arguments of main() are the command-line arguments of the program. So if you do for example this in Python:
subprocess.Popen(['myCppprogram.exe', 'foo', 'bar'], ...)
then the following will hold in main():
int main(int argc, char** argv)
{
assert(argc == 3);
assert(argv[1] == std::string("foo");
assert(argv[2] == std::string("bar");
}
According to what i have understood from your question, you want to call a .exe file from python and pass arguments to the C++ file.
import subprocess
program = 'path to your exe file'
argument = "0"
subprocess.call([program, argument])
This will execute the .exe from python and the arguments passed can be read in C++ main as members of the array argv.

Communicating between a ruby script and a running c++ program

I have a c++ program which performs one function. It loads a large data-file into an array, receives an array of integers and performs a lookup in that array, returning a single integer. I am currently calling the program with each integer as an argument, like so:
$ ./myprogram 1 2 3 4 5 6 7
I also have a ruby script, and I would like this script to utilize the c++ program.
Currently, I am doing this like so.
Ruby Code:
arguments = "1 2 3 4 5 6 7"
an_integer = %x{ ./myprogram #{arguemnts} }
puts "The program returned #{an_integer}" #=> The program returned 2283
This is all working properly, but my problem is that each time ruby makes this call, the c++ program has to reload the data-file (which is over 100mb) - very slow, and very inefficient.
How can I rewrite my c++ program load the file only once, allowing me to make many lookups via a ruby script without reloading the file each time. Would using sockets be a sensible approach? Writing the c++ program as a ruby extension?
Obviously I am not an experienced c++ programmer, so thanks for your help.
A possible approach is to modify your C++ program so that it takes its input from the standard input stream (std::cin) instead of from the command line parameters, and returns its result through the standard ouput (std::cout) instead of as main's return value. Your Ruby script would then use popen to launch the C++ program.
Assuming the C++ program currently looks like:
// *pseudo* code
int main(int argc, char* argv[])
{
large_data_file = expensive_operation();
std::vector<int> input = as_ints(argc, argv);
int result = make_the_computation(large_data_file, input);
return result;
}
It would be transformed into something like:
// *pseudo* code
int main(int argc, char* argv[])
{
large_data_file = expensive_operation();
std::string input_line;
// Read a line from standard input
while(std:::getline(std::cin, input_line)){
std::vector<int> input = tokenize_as_ints(input_line);
int result = make_the_computation(large_data_file, input);
//Write result on standard output
std::cout << result << std::endl;
}
return 0;
}
And the Ruby script would look like
io = IO.popen("./myprogram", "rw")
while i_have_stuff_to_compute
arguments = get_arguments()
# Write arguments on the program's input stream
IO.puts(arguments)
# Read reply from the program's output stream
result = IO.readline().to_i();
end
io.close()
Well,
You could go about this a number of different ways.
1) A simple, potentially ugly way to do this is to have your c++ run and intermittently check for a file, have your ruby script produce said file containing your arguments. Your C++ program would then use the contained arguments returning it's result to a result file which you could wait on within your ruby script... This is obviously HACK TASTIC but it's uber simple to implement and would work.
2) Expose your c++ code as a c extension to ruby. This is not as hard as it's sounds especially if you use RICE and would provide significantly less hackie solution.
3) If your c++ can be exposed through a c header file then it's almost trivial to construct a bridge using FFI. Jeremy Hinegardner gave a good lecture on constructing FFI interfaces at rubyconf heres the screencast
4) D-Bus provides an application communication bus, you could alter your C++ app to take advantage of said event bus and pass messages from your ruby using ruby-dbus
There are of course a thousand other routes... Maybe one or the other of these could prove viable :)
Cheers!

usage of system function in cpp program

Please explain the syntax of:
system(const char *command);
I want to use this function for running the command on unix sytem.
I need to execute(automate) several test cases with the same command but,they also have other input values which are different.how do I reuse this code for all the test-cases.
int main()
{
char *base = "./your_testcase " ;
char aux[50] = "./your_testcase " ;
char *args[] = {"arg1" ,"arg2" ,"arg3"};
int nargs = 3;
for(i=0;i < nargs;i++)
{
/* Add arg to the end of the command */
strcat(aux,args[i]) ;
/* Call command with parameter */
system(aux);
/* Reset aux to just the system call with no parameters */
strcpy(aux,base);
}
}
Keep in mind that calling system is the same as calling fork and execl. That mean you need to be aware of things like open socket descriptors and file descriptors. I once had a problem with a TCP/IP socket dying on a server because a client was calling system which created a new socket connection to the server that was not being serviced.
I don't see how the syntax can be a problem:
system( "foo" );
executes the program called foo, via your preferred shell.
Generate a command line for each invokation, then pass those command lines into system() one a time.
See also the question:
'How to call an external program with parameters?;
How to call an external program with parameters?
I would avoid use of the system() function, here is a link to why this might be a bad idea
Here is the code, how to implement system() command in c++
#include <cstdlib>
 
int main()
{
    system("pause");
    return 0;
}
system(const char *command)
Is used to execute a command on the command line of the current operating system. It is generally not the best idea to use this because the commands are platform specific. Keep in mind that const char *command is a string and you can pass any string value as a parameter and it will be sent to the command line.
i think Anter is interested in a example:
for instance to remove a file in a directory:
system("/bin/rm -rf /home/ederek/file.txt");