How do you display the command arguments in Visual Studio? - c++

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.

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 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.

Giving the application some data on launch

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

problem with reading program arguments in Visual Studio C++

I'm running C++ program in VS2005, and I've set only one argument in project properties-> debug-> command line args, and it's named profile1.dll for example.
here's a code snippet
cout<<"number of arguments:" << argc<<endl;
for (int i=0; i<argc; i++)
cout << "argument " << i << ": " << argv[i] << endl;
In the output I get
number of arguments:2
argument 0: c
argument 1: p
don't know why it doesn't print the name of the argument?
Does the name of your exe start with C? If you expect a string and you only get one character, it's usually because you've discovered that the Western alphabet in UTF-16 Unicode effectively puts a 0 between alternating ANSI chars. Are you compiling for Unicode ?
argv[0] is the name of your program. argv[1] is the first parameter. It sounds like you have declared the relevant parameter in main() as char* argv rather than char *argv[] or char **argv.
Leave TCHAR be, it's fine.
If you compile with unicode, use wcout for output:
int _tmain(int argc, _TCHAR* argv[])
{
for (int i=0; i<argc; i++)
wcout << "argument " << i << ": " << argv[i] << endl;
return 0;
}
Otherwise compile without unicode and your code will just work as-is (and will not work with unicode parameters :o)
You can find setting in "Project Properties/General/Character Set"
Can you put the prototype of your main function? What you are doing is apparently fine.
Make sure your main's function prototype is something similar to:
int main(int argc, char **argv)
Hope it helps.