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

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.

Related

c++ - How to create commands with variables to be executed in cmd

Let me specify the question. I want to make commands so when user will type eg. go (something) it will use the go to specify the function and something to find what the program should do when variable equals something. If there is anything unclear, just ask, i know my explanation is strange.
Example:
In program there is a void go(string choice) function with if condition including few variables that can be used through choice string.
User is opening program and typing go and then string. Program goes to function go and if string is included in if loop, then program goes to specific if condition and does what it should eg.:
if(choice == "room")
{
//condition being executed
}
So basically you want a specific function to be executed according to a cmd line input? Easy! Create you main function like this:
int main(int argc, char *argv[])
argc is the argument count and argv is the argument value. Google up on this if you need more info, it's widely used. Use an if statement inside your main function that will call the required function according to the value of argv[]. So when you execute your program you'll execute like
exe_file_name go anything
If you want to call multiple functions dynamically during a single execution you could instead do
getline(cin,choice);
if(strcmp(choice, "room")) //condition
I think it should be like
if(strcmp(choice , "room")){//condition being executed}

Get parameter from exe in 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.

C++ VS2010 not using my Debug command line args

In VS2010 I set command line arguments in the project settings->Debugging->Command line arguments:
-d 48000 1 -raw test1.opus test1_decoded.raw
However, when I debug the project and have a look at the argv[] in
int main(int argc, char *argv[])
{
}
... I can see that these command line arguments are missing.
The command line argument argv instead has only the path to the exe that is just being debugged. I see that if I move the mouse over the argv.
Does anybody have an idea what I might have done wrong?
Thank you for the help.
However, when I debug the project and have a look at the argv[]...
Per your description and code, I'm assuming that you are hovering your mouse over argv or looking at it in the watch window. argv is a pointer to pointer to char. The debugger does not know how many elements it contains. It will show you the first element i.e.,*argv`), but no more because there is simply no safe, standard way of doing so.
Your command line arguments are there, but the debugger cannot figure out how many elements to display in the UI. Look at the value of argc; that should match your number of arguments +1 for the path to your executable.

Executing program with an DAT file and input

How am I able to execute my C++ program together with a .DAT file and an input?
For example:
./program.exe file.dat 5
Example of multiple command lines in file.dat:
addpeer 12130
removepeer 13820
In C++, your use the main function int main(int argc, char *argv[]), argc contains the "argument count" i.e. the number of your arguments, and argv is a vector containing the arguments you provided when you called your program. In your case it will contain "file.dat" and 5. Once you have this you can parse your file with your program and do what you want with it.

When opening a program in a Unix terminal, is there a way to have the program name and its input on the same line?

I have a C++ program that accepts three inputs: an integer for width, an integer for height, and a filename. Right now, I compile, and run the program like this (assuming I named it prog):
>prog
// hit enter
>128 128 output.ppm
This results in a successful output, but the program description says the proper command-line syntax is:
>prog w h filename
That's all it says. Does this imply that my program should be able to start on the same line? Perhaps it implicitly means you hit enter after typing the program name, but if not, is there a way to actually do this?
Your program needs to parse command line parameters. Looking at the specification, the expected workflow is
>prog 128 128 output.ppm
//hit enter after the parameters
Look here to learn more.
You're approaching the problem incorrectly. You are taking your input via std::cin after your program has been started. Your program specification states that the input should be given as part of the command. Consider a command like ls -l - the -l is part of the command and is passed to the program to parse and act upon.
You need to allow a command like prog 128 128 output.ppm to be run, so the user would type that and then press enter to run the program. How do you get access to the command line arguments within your C++ program? Well, that's what the argc and argv parameters of the main function are for. Your main function should look like this:
int main(int argc, char* argv[]) { ... }
The argc argument gives you the number of arguments passed in the command line (it will be 4, in the example given) which is also the size of the argv array. Each element is an argument from the command. For example, argv[0] will be "prog", argv[1] will be "128", and so on. You need to parse these values and, depending on their values, change the functionality of your program.
You can pass command via the argument in the main function:
int main(int argc, char *argv[]) {
}
argc is the number of arguments and argv is an array of arguments.