I've been working with OpenCV, and some of the example code I've seen uses the following to read in a filename. I understand that argc is the number of command line arguments that were passes, and argv is a vector of argument strings, but can someone clarify what each part of the following line does? I've tried searching this but haven't found many results. Thanks.
const char* imagename = argc > 1 ? argv[1] : "lena.jpg";
Thanks.
const char* imagename = // assign the string to the variable 'image_name'
argc > 1 // if there is more than one cmd line argument (the first is always the program name)
? argv[1] // use the first argument after the program name
: "lena.jpg"; // otherwise use the default name of "lena.jpg"
If argc is greater than 1, assigns to imagename the pointer held in argv[1] (i.e. the first argument given on the command line); otherwise (argc is not greater than 1), assigns a default value, "lena.jpg".
It uses the ternary operator ?:. This is used this way: CONDITION ? A : B and can be read as
if (CONDITION)
A
else
B
So that a = C ? A : B assigns A to a if C is true, otherwise assigns B to a. In this specific case, "A" and "B" are pointers to char (char *); the const attribute says we have "strings" that are "constant".
The example shows the use of the ternary operator.
const char* imagename = argc > 1 : argv[1] : "lana.jpg"
By ternary you can say that this expression has three members.
First member is a condition expression
Second member is the value that could be assigned to imagename if conditional expression is true.
Third member is the value that could be assigned to imagename if conditional expression is false.
This example can be translated to:
const char* imagename;
if(argc > 1)
imagename = argv[1];
else
imagename = "lana.jpg";
if (argc > 1) {
const char* imagename = argv[1];
} else {
const char* imagename = "lena.jpg";
}
(if we agree that imagename can go out of the brackets' scope)
Related
I am trying to write my own string class for an assignment, and I was wondering how I should treat the argument of "".
For example, if there is a call of:
s = myString("")
what is the length, and what are the contents of the char* holding the data in my 'myString' class?
The char * passed to you will be a pointer to an "null" terminated list of char's which, most likely, will be a single, immutable char whose value is 0 (or "null").
For example...
const char* s = "";
char value = *s;
int length = strlen(s);
... should result in...
s == [compiler defined]
!value == true
length == 0
I know this question is basic, I also newbie, so plz help me clear this problem:
I have this code:
int wmain(int argc, wchar_t *argv[])
{
if (*argv[1] == L'-' && (!_wcsicmp(L"install", argv[1]+1)))
{
wprintf(L"You pressed: -install !");
}
else
{
wprintf(L"Unknown");
}
}
It work OK, but if I replace to:
int wmain(int argc, wchar_t *argv[])
{
if (*argv[1] == L'-' && (!_wcsicmp(L"install", argv[2])))
{
wprintf(L"You pressed: -install !");
}
else
{
wprintf(L"Unknown");
}
}
It's break when run.
I don't know
Why use argv[1]+1
What difference between argv[1]+1 and argv[2] (I've used above).
Anyone explain for me, and give me document about wchar_t ???
First,
Why use argv[1]+1:
--> _wcsicmp require POINTER, so you need pass: argv[1] + 1 or argv[2]
Second,
What difference between argv[1]+1 and argv[2] (I've used above). :
-->
Do you see second argument of wmain: wchar_t *argv[] ? - This is mean POINTER to ARRAY (same same POINTER to POINTER).
Each argument separate by space.
So, argv[1] is pointer point to second argument, argv[2] is pointer point to third argument.
And, argv[1]+1 increase address in pointer by 1 (mean, that pointer will point to second character in second argument string)
argv[1]+1 points to the second character in the argv[1] string, while argv[2] points to the next string.
argv[1] + 1 is a pointer to the second character in argv[1] while argv[2] is the third element (pointer to character) in argv.
argv[1] == *(argv + 1)
argv[2] == *(argv + 2)
argv[1] + 1 == *(argv + 1) + 1 != argv[2]
Just a simple problem here: I have a char** argv[] that holds all of my arguments...in one of these arguments, I get an integer proceeded by a %
For example:
bg %2
I really just want the integer....is there an easy way to get this?
This is for homework, so I am willing to do some more digging if anyone can prod me in the right direction.
Thanks
Here is a way to do it using c++ methods:
lets assume you have one of the char* in the list char** argv[]
std::string tempString(argv[the one with the %]);
int position = tempString.find_first_of('%');
int = atoi(tempString.substr(position, tempString.size()-position).c_str());
A quick explination, the first line converst the char* into a std::string, the second line gets the position of the %, the third line gets the sub-string of the number (assuming it ends at the end of the char*), converts it back to a char* and passes it through atoi to get the int.
Hope this helps.
Here is one way to do it using atoi:
for (int i = 0 ; i != argc ; i++) {
if (argv[i][0] == '%') {
int num = atoi(&argv[i][1]);
printf("Got a number: %d\n", num);
}
}
I have code which does the following :
const char* filename = argc >= 2 ? argv[1] : "stuff.jpg";
which reads in a photo as a command line argument and displays it.
I now want to take in two photo's, I tried this code :
const char* filename = argc >= 3 ? argv[1] : "stuff.jpg", argv[2] : "tester.jpg";
But I get an error like this :
error: expected initializer before ‘:’ token
Anybody know whats wrong? is there a similer way to do this input programmatically?
You're dealing with a ternary if-operator here. Have a look at this page. It's basically an inline if-statement.
Code that would do what you're looking for, looks something a little like this:
const char* filename1 = argc >= 2 ? argv[1] : "stuff.jpg";
const char* filename2 = argc >= 3 ? argv[2] : "tester.jpg";
That leaves you with two filename variables, storing either the supplied argument or the default values (stuff.jpg and tester.jpg, respectively).
To get all the arguments in an easy to use format I do:
int main(int argc, char* argv[])
{
std::vector<std::string> args(&argv[1], &argv[argc]);
// args.size() is the number of arguments.
// In your case the number of files.
// So now you can just loop over the file names and display each one.
// Note The above is guranteed to be OK
// As argv[] will always have a minimum of 2 members.
// argv[0] is the command name thus argc is always >= 1
// argv[argc] is always a NULL terminator.
}
What happens when you need 4, 5, or more photos?
Pseudo code:
vector<char *> photos;
if(argc > 1)
{
for i to argc-1
photos.push_back(argv[i]) ;
}
for example I think of a situation like this:
string param1 (argv[i+1]);
and I know that it is possible that this may look out of array length, is there a way to declare in a program that whenever I put statement like this a default value will be assigned, something like obviously incorrect string param1 (argv[i+1] or "default");
What about:
string param1((i < argc-1) ? argv[i+1] : "default");
Um, well, the obvious.
char const * avi1 = "default";
if (argc > (i + 1)) {
avi1 = argv[i+1];
}
string param1(avi1);
Short answer: no.
If you know that this may look outside of array length, check it manually.Something like
string param1((i < argc-1) ? argv[i+1] : "default");