By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,496 Members | 1,266 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,496 IT Pros & Developers. It's quick & easy.

argv[2] comparing with "d"

P: n/a
i have a quick question

i am putting a debug flag in my program (i really dont need this
feature, but i figured it might be useful when i get into trouble)

so i want to check if argv[2] is the letter "d"

this is what i have so far

if (argv[2]) { write_read_input_file(filename); }

(it works, as long as there's more than 1 argument to the program, it
works. good enough for a lousy flag which i don't need, but i am not
satisfied)

i tried doing the following

argv[2] == "d" //compiles, but doesn't work
argv[2] == 'd' //can't do this
strcmp(argv[2], "d") //segmentation fault

God I wish c++ was like perl. :-)

Sep 10 '05 #1
Share this Question
Share on Google+
9 Replies


P: n/a
<ma******@gmail.com> wrote in message
news:11**********************@g49g2000cwa.googlegr oups.com...
i have a quick question

i am putting a debug flag in my program (i really dont need this
feature, but i figured it might be useful when i get into trouble)

so i want to check if argv[2] is the letter "d"

this is what i have so far

if (argv[2]) { write_read_input_file(filename); }

(it works, as long as there's more than 1 argument to the program, it
works. good enough for a lousy flag which i don't need, but i am not
satisfied)

i tried doing the following

argv[2] == "d" //compiles, but doesn't work
argv[2] == 'd' //can't do this
strcmp(argv[2], "d") //segmentation fault

God I wish c++ was like perl. :-)


argv[2] should be a char*. That is, it points somewhere in memory.

argv[2] == "d" wouldn't work, because you are comparing wehre argv[2] is
pointing at to where the constant char array "d" is stored, which are of
course different.

Same as for argv[2] == 'd' although now you're comparing a pointer to a
character (integer value).

strcmp(argv[2], "d") should work.

I think you are confused though. The second parameter is argv[1], not
argv[2], since arrays in c++ are 0 based. This works for me with the
comamnd line parmaters passed: "xxxx d"

#include<iostream>
int main(int argc, char* argv[])
{
std::cout << "Arguments: " << argc << std::endl;
std::cout << "Arg 0:" << argv[0] << std::endl;
if ( argc > 1 )
std::cout << "Arg 1:" << argv[1] << std::endl;
if ( argc > 2 )
{
std::cout << "Arg 2:" << argv[2] << std::endl;
std::cout << "Art 2 is \"d\":" << strcmp(argv[2], "d") << std::endl;
}
return 0;
}

Output is:
C:\temp\console\Debug>console xxx d
Arguments: 3
Arg 0:console
Arg 1:xxx
Arg 2:d
Art 2 is "d":0

Notice: Different OSes may pass the first argument (argument 0) as the
executable name used to execute the program. This may also include the
path.

Notice: strcmp() returns 0 if they are equal.

HTH
Sep 10 '05 #2

P: n/a
>
God I wish c++ was like perl. :-)


You think learning C++ is difficult after knowing Perl, try imagining
what it is like the other way round. God I wish perl didn't seem like
gibberish (I'm sure it isn't but it sure seems like that when compared
to C++).

john
Sep 10 '05 #3

P: n/a
:-)

open(INFILE, "filename.txt");
@myarray = <INFILE>;

an entire file is read into an array, with each line as each element of
that array. that's one of the reasons why i love perl. :-)

i actually learned a bit of c++ and then moved to perl and i like how
it is not very anal about syntax and what not so i got used to it.

but i definitely see your point. i felt the same when i first learned
perl. :-) it still is if i am reading someone else's code.
coming back to the topic:
i got it to finally work with strcmp

i just had to make sure 2nd argument was there before using with strcmp
(otherwise i'd get a segmentation fault)

if (argv[2] && strcmp(argv[2], "d") == 0) { cout << "got here" << endl;
}

thanks

John Harrison wrote:

God I wish c++ was like perl. :-)


You think learning C++ is difficult after knowing Perl, try imagining
what it is like the other way round. God I wish perl didn't seem like
gibberish (I'm sure it isn't but it sure seems like that when compared
to C++).

john


Sep 11 '05 #4

P: n/a
>
coming back to the topic:
i got it to finally work with strcmp

i just had to make sure 2nd argument was there before using with strcmp
(otherwise i'd get a segmentation fault)

if (argv[2] && strcmp(argv[2], "d") == 0) { cout << "got here" << endl;
}


I think that's probably still not correct, the argc parameter is there
to test how many command line arguments you have, i.e.

int main(int argc, char** argv)
{
if (argc >= 3 && strcmp(argv[2], "d" == 0)
{
...

john
Sep 11 '05 #5

P: n/a
ma******@gmail.com wrote:
:-)

open(INFILE, "filename.txt");
@myarray = <INFILE>;

an entire file is read into an array, with each line as each element of
that array. that's one of the reasons why i love perl. :-)


I like a challenge, from memory (i.e. I haven't compiled this)

#include <fstream>
#include vector>
#include <iterator>
#include <algorithm>
#include <string>

ifstream infile("filename.txt");
vector<string> myarray;
copy(istream_iterator<string>(infile),
istream_iterator<string>(),
back_inserter(myarray));

Not quite as concise or as flexible as the perl code, but it's the
closest C++ can get. It doesn't do quite the same thing either because
the input file will be split on any whitespace not just end of lines.
But you could get exactly the same effect if you wrote your own version
of the istream_iterator class.

john
Sep 11 '05 #6

P: n/a

<ma******@gmail.com> wrote in message
news:11*********************@g44g2000cwa.googlegro ups.com...
[snip]
i got it to finally work with strcmp

i just had to make sure 2nd argument was there before using with strcmp
(otherwise i'd get a segmentation fault)

if (argv[2] && strcmp(argv[2], "d") == 0) { cout << "got here" << endl;
}

[snip]

I wouldn't do that. if (argv[2]... is checking to see if the argv[2]
pointer is a null pointer, which is UB (undefined behavior) if you don't
have 3 or more arguments. Anything could be in there.

Use argc, the argument count, to see if you have at least 3 arguments. If
you have at least 3 arguments, argv[2] is valid, other wise it's not.

if (argc > 2 && strcmp(argv[2], "d" == 0) {...

Sep 11 '05 #7

P: n/a
Jim Langston wrote:
<ma******@gmail.com> wrote:

if (argv[2] && strcmp(argv[2], "d") == 0) { cout << "got here"
<< << endl; }
I wouldn't do that. if (argv[2]... is checking to see if the argv[2]
pointer is a null pointer, which is UB (undefined behavior) if you
don't have 3 or more arguments. Anything could be in there.


argv[argc] is always a null pointer (ie. the original code
is valid if there are exactly two arguments).

Sep 11 '05 #8

P: n/a
"Old Wolf" <ol*****@inspire.net.nz> wrote in message
news:11*********************@o13g2000cwo.googlegro ups.com...
Jim Langston wrote:
<ma******@gmail.com> wrote:

if (argv[2] && strcmp(argv[2], "d") == 0) { cout << "got here"

<< << endl; }

I wouldn't do that. if (argv[2]... is checking to see if the argv[2]
pointer is a null pointer, which is UB (undefined behavior) if you
don't have 3 or more arguments. Anything could be in there.


argv[argc] is always a null pointer (ie. the original code
is valid if there are exactly two arguments).


Okay, but what happens when you do this... you add a debug flag
as parameter 2 and compare if (argv[2]... Later you decide to
add another possible flag as parameter 3 and do if (argv[3]...

If you don't add 2 or 3, UB
Sep 11 '05 #9

P: n/a
yeap. argv[2] was changed to argc >= 3

the more i read stuff here, the more i find out i am lacking in a lot
of common sense.

Sep 11 '05 #10

This discussion thread is closed

Replies have been disabled for this discussion.