Executing program with an DAT file and input - c++

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.

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.

passing on inputs to an exe file using c++

lets say we have a cal.exe file (a simple addition calculator programmed programmed in c++).
lets say that the console output screen first displays enter the first number: and waits for the user to input an integer value. I am willing to create a c++ program that would "pass on" the required value to the running process (cal.exe) as an input (playing the role of a user). I would also like to have the output from the cal.exe file to be displayed and interpreted by my program.
I havent got the slightest idea how to proceed with this. Is there any open source library that would help me accomplish this? If there is, could you name a few?
I have just learned object oriented programming in c++ last year in my school and I am not used to these kind of stuff in programming; so please excuse me if this question is silly.
update:
lets consider 2 processes a.exe and b.exe running. could you tell me a possible way to program b.exe which provides a integer input to a.exe (a console process) as if it was from the user?
You can do this by accepting command line arguments.something like this
int main ( int argc, char *argv[] )
{
enter code here
return 0;
}
Where,
First argument to main function (argc) refers to the number of arguments being passed to the program at run-time.
Second (char *argv[] )refers to a string containing the arguments that are passed (char * is treated as String also ).
Argument names may vary as per the user specifications.
For details Refer:
http://www.cplusplus.com/articles/DEN36Up4/
And for nesting of programs you can use system("name of child program goes here") Function under stdlib.h.
For details Refer:
http://www.cplusplus.com/reference/cstdlib/system/

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}

Using Command Line Arguments To Input Parameters

So I have a Windows Studio VS File named RectArea that contains the code to a function to find the area of a rectangle. In another Windows Studio VS file named main1, I have my main function like this:
int main(int argc, char *argv[])
{
return 0;
}
How do I use command line arguments to be able to print the area of the rectangle in the main file as soon as i compile it?
I read so much stuff about it online. I just still don't understand what I put for the executable and the stuff after it.
My function for the rectangle's area is in the RectArea file. Do I type in C://RectArea?
It's under a lot more folders though.
Ah, you're confused on how to pass the arguments to the program, not how to interpret them. Those arguments are not command line arguments passed to the Visual Studio build process; they're arguments that can be changed each time you run the program.
An executable written with an entry point like that can't be run properly via double-click like all the others you've written; you have to open cmd.exe and run the program from there, typing the arguments after the program name separated by a space.

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.