473,505 Members | 13,599 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

argv[2] comparing with "d"

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
9 5905
<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
>
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
:-)

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

<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
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
"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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
4304
by: wukexin | last post by:
Help me, good men. I find mang books that introduce bit "mang header files",they talk too bit,in fact it is my too fool, I don't learn it, I have do a test program, but I have no correct doing...
25
3055
by: Nitin Bhardwaj | last post by:
Well, i'm a relatively new into C( strictly speaking : well i'm a student and have been doing & studying C programming for the last 4 years).....and also a regular reader of "comp.lang.c" I...
14
3742
by: spike | last post by:
Im trying to write a program that should read through a binary file searching for the character sequence "\name\" Then it should read the characters following the "\name\" sequence until a NULL...
0
1311
by: olimpia | last post by:
Hello, I am getting back to programming after many many years. Never work with Basic and now I am trying to work on a little project using Visual Basic.NET 2003 to verify info (at least 10 chars)...
7
8490
by: Eric | last post by:
Hi For this code, int getopt (int argc, char *const argv, const char *opts) what does the "char *const argv" mean? Does it equal to "char **const argv"? Or "char *const *argv"? Which is the...
1
2565
by: poggle.themammal | last post by:
The python tutorial says "When the script name is given as '-' (meaning standard input), sys.argv is set to '-'. When -c command is used, sys.argv is set to '-c'. " but when we use a command say...
9
2664
by: Cao Yi | last post by:
Hi, here's a fract of codes, and what's the line "scanf("%lf%*", &cvi)" doing? ============================= do { printf("\nCoefficient: "); scanf("%lf%*", &cvi); getchar(); } while (cvi <=...
0
977
by: DomiNeug | last post by:
Hello, Since a while i have to find a way of comparing "Sets" (multiple int Values) and so to find equal sets. There simply 3 tables ValueList with: ID int ValueListHasValue: ID int, ValueListID...
1
2150
by: nicstel | last post by:
I don't understand the "argv" in TKinter module Can you tell me what argv means and do? Thank You
0
7098
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7366
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7017
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7471
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5610
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4698
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3176
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
754
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
406
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.