Giving the application some data on launch - c++

I'm trying to create an application that can receive some data on launch from another program.
For example:
Start_App.exe calls Main_App.exe and gives it the current date, all at the same time
(while launching it)
Main_App.exe outputs the date on its console
Without the data passed by Start_App the other program can't work correctly or will do something else.
I've been searching for a while but it seems like I'm missing the
technical names...

You'll probably want to use command-line arguments.
They are passed by writing them out, separated by spaces, directly after the program name.
Like so:
#include <iostream>
int main(int argc, char *argv[])
{
using namespace std;
cout << "There are " << argc << " arguments:" << endl;
// Loop through each argument and print its number and value
for (int nArg=0; nArg < argc; nArg++)
cout << nArg << " " << argv[nArg] << endl;
return 0;
}
argc is the number of arguments the program received.
*argv[] is an array of strings, one for each argument.
If you call the program like this:
Program.exe arg1 arg2 arg3
It gives you:
There are 3 arguments:
0 arg1
1 arg2
2 arg3

Related

Command line arguments not passing through correctly, argc always returning 1 and argc returning nullpointer on visual studio c++

I was recently working on a project for a class and was having a lot of problems with passing in command line arguments. I decided to test out with a really simple code I found online from geeksforgeeks to see if I could get any sort of command line stuff to work and it still is not working. It will not print any argv values and when I debug it, it says that argc is 1 despite me putting in 4 command line arguments. I have been trying to find answers to this online for hours and have no idea what is going on especially when using this really simple code. I attached the code I was testing below. It only prints out "You have entered 1 arguments:" I am relatively new to coding but very confused.
#include <iostream>
using namespace std;
int main(int argc, char** argv)
{
cout << "You have entered " << argc
<< " arguments:" << "\n";
for (int i = 0; i < argc; ++i)
cout << argv[i] << "\n";
return 0;
}

Passing bash parameters to c++

I tried passing two parameters from bash to c++ program to be used but cant seem to get it to work exactly right. I would use the command ./bash "Turtle" "Cat" in linux command line.
#!/bin/bash
./main.out $1 $2
But the C++ file would only read ./main.out from argv. The cout would just print cut off versions of ./main.out such as ./main.out then /main.out and then main.out. Am I incorrectly using the parameters in the placement of the bash file?
int main(int argc, char *argv[]) {
cout << argv+0 << endl;
cout << argv+1 << endl;
cout << argv+2 << endl;
return 0;
}
This is because you are not printing the arguments as strings, your printing their memory locations. In clang++-9 (what I tested it in) this is what happens when a pointer is passed to std::cout, in many compilers (MSVC, correct me if im wrong) this will simply print nothing.
What you need to do is reference it as an array index and print that
int main(int argc, char **argv) {
cout << argv[0] << endl;
cout << argv[1] << endl;
cout << argv[2] << endl;
return 0;
}
The above code works fine for me (passing arguments manually when executing) when compiled with clang++-9. If you are going to use this code you should also check that there are at least 3 arguments (value of argc) otherwise you may point at invalid memory when indexing argv
Also try and avoid std::endl and use "\n" instead, std::endl needlessly flushes the buffer and is not required 99% of the time
Your bash file is fine. The problem is in your C++. Try something like this:
#include <iostream>
int main(int argc, char **argv) {
std::cout << argv[1] << "\n";
std::cout << argv[2] << "\n";
}
[warning: this doesn't check for errors, so if you don't pass any parameters, it'll misbehave badly.]
char *argv[] is a pointer of pointers (same as char **argv), you need
cout << argv[1] << endl;

Passing parameters from command line to C++

Searching the net for examples how to pass command line parameters to a C++ code, I came up with an abandoned post where this process is being explained. This code was not working and after a few amendments I came up with the following (working) code:
#include <iostream>
#include <windows.h>
#include <fstream>
#include <string>
using namespace std;
// When passing char arrays as parameters they must be pointers
int main(int argc, char* argv[]) {
if (argc < 2) { // Check the value of argc. If not enough parameters have been passed, inform user and exit.
std::cout << "Usage is -i <index file name including path and drive letter>\n"; // Inform the user of how to use the program
std::cin.get();
exit(0);
} else { // if we got enough parameters...
char* indFile;
//std::cout << argv[0];
for (int i = 1; i < argc; i++) { /* We will iterate over argv[] to get the parameters stored inside.
* Note that we're starting on 1 because we don't need to know the
* path of the program, which is stored in argv[0] */
if (i + 1 != argc) {// Check that we haven't finished parsing already
if (strcmp(argv[i],"/x")==0) {
// We know the next argument *should* be the filename:
char indFile=*argv[i+1];
std::cout << "This is the value coming from std::cout << argv[i+1]: " << argv[i+1] <<"\n";
std::cout << "This is the value of indFile coming from char indFile=*argv[i+1]: " <<indFile <<"\n";
} else {
std::cout << argv[i];
std::cout << " Not enough or invalid arguments, please try again.\n";
Sleep(2000);
exit(0);
}
//std::cout << argv[i] << " ";
}
//... some more code
std::cin.get();
return 0;
}
}
}
Executing this code from the Windows command line using:
MyProgram.exe /x filename
returns the next output:
This is the attribute of parameter /x: filename
This is the value from *argv[i+1]: f
The original post from cplusplus.com did not compile; the code above does.
As you can see printing the argv[2] gives me the name of the file. When I try to capture the file name into another var so I can use it in the C++ program, I only get the first character (second response line).
Now for my question: How can I read the value from the command line parameter the pointer is pointing to?
Hope someone can help this newbie in C++ :-)
*argv[i+1]
Accesses the 1st char of the char* argv[] argument.
To get the whole value use something like
std::string filename(argv[i+1]);
instead.
You can't store a string in a single char.
Here's the once-an-idiom for copying the main arguments to more manageable objects:
#include <string>
#include <vector>
using namespace std;
void foo( vector<string> const& args )
{
// Whatever
(void) args;
}
auto main( int n, char* raw_args[] )
-> int
{
vector<string> const args{ raw_args, raw_args + n };
foo( args );
}
Do note that this code relies on an assumption that the encoding used for the main arguments can represent the actual command line arguments. That assumption holds in Unix-land, but not in Windows. In Windows, if you want to deal with non-ASCII text in command line arguments, you'd better use a third party solution or roll your own, e.g. using Windows' GetCommandLine API function.

How do you display the command arguments in Visual Studio?

I have a project in C++ (using Visual Studio 2013), and I know that if i want to pass command arguments I have to go to Project > Properties > Configuration Properties > Debugging and then type the command in "Command Arguments".
But I'd like to see those command arguments in the actual program (after I click "Start without debugging"), since I can only see the output, without the command.
int main (int argc, char *argv[])
{
for (int i = 1; i < argc; i++)
std::cout << "argument " << i << " = " << argv[i] << std::endl;
}
#include <iostream>
int main(int argc, char* argv[])
{
for(int i = 0; i < argc; ++i)
{
std::cout << "arg[" << i << "]: " << argv[i] << std::endl;
}
return 0;
}
Output:
arg[0]: C:\VS2015\PrintCmdArgs\Debug\PrintCmdArgs.exe
arg[1]: here
arg[2]: are
arg[3]: some
arg[4]: arguments
Press any key to continue . . .
There are several ways you can "see" the command arguments once the program has started running.
Simply print them from main.
If you need to display them in a form or print them while the application is running then you'll need to copy the arguments to some global/static variable and then access that when you need to.

Passing parameters to a function from the Bash command line

I am running a C++ program from the command line on Bash, which is in a Linux environment. I am curious how you pass in a parameter from the command line. Here is my program:
#include <iostream>
using namespace std;
int large_pow2( int n );
int main()
{
int value = 15;
int largest_power = large_pow2(value);
cout << "The highest power of 2 in " << value << " is " << large_power << "." << endl;
return 0;
}
int large_pow2( int n )
{
int i = n
int j = i & (i - 1);
while( j != 0)
{
i = j;
j = i & (i - 1);
}
return j;
}
After I compile the program I want to be able to use the command line to pass in a number to use for value. For instance, to run the program you type ./"program_name" where "program_name" is the name of my program without quotes. Is there a way to set value = n or something? When I run the program let's say I want n to be 20 so on the command line I type something like ./"program_name" 20. Then the program would run with n = 20. Is there a way to do that? I am completely new to a Linux environment and Bash so don't quite know how to do things in it yet.
Use argc and argv in int main(int argc, char *argv[]) and modify your code accordingly.
The argc arguments tracks the number of arguments passed to your program from CLI and is always >=1. When 1 it is it name of program. So argc[0] is program name.
argv holds the command line arguments, other than program name and is always char string. Hence we need to use appropriate converter like atoi, if you don't want string.
So your code will look like, error checking not done for simplicity
int main(int argc, char *argv[])
{
//Now we expect argc ==2, program value, This will when argc != 2
// We should use if in real scenario
assert(argc == 2);
int value = atoi(argv[1])
int largest_power = large_pow2(value);
cout << "The highest power of 2 in " << value << " is " << large_power << "." << endl;
return 0;
}
Your main method can take (int argc, char** argv) which are the count of arguments and the NUL terminated args. The program path is argv[0] so atoi(argv[1]) is probably what you want. Check argc ==2.