about command line arguments


I want to print out a report that report the total number of lines if the user enters "-r" as the first argument :

I tried it this way but the program will not enter if the statement:


ctr is just a counter that counts the total number of lines in the program.


1
2
3
4
5
6
7
8


 if( argv[1] == "-r")
 {
    cout << "number of lines is " << ctr << endl;
 }

Use strcmp (http://www.cplusplus.com/reference/cstring/strcmp/) for the C-string comparisons. It needs header <cstring>. I always get confused which way round is true and which is false (actually, it returns integers 0 or 1, which implicitly convert to false or true). You probably need !strcmp here.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <fstream>
#include <cstring>
#include <string>
using namespace std;


int countLines( char *filename )
{
   ifstream in( filename );   if ( !in ) return -1;        // flags file not opened
   int ctr = 0;
   string dummy;
   while ( getline( in, dummy ) ) ctr++;
   in.close();
   return ctr;
}


int main( int argc, char *argv[] )
{
   if ( argc >= 3 && !strcmp( argv[1], "-r" ) ) cout << countLines( argv[2] );
}


test -r test.cpp
22
Last edited on
will something like this work if( argv[1] == "-r") ?
no, because the command line args are c-strings.

You either need to string it up, or just use c-string code.
so you want if strcmp(argv[1], "-r" == 0)

or
string argv1 (argv[1]);
if (argv1 == ....

there is no comparison operator for char-array (c-string) "type".
Last edited on
i tried running the program through the command prompt and it works fine.
but if i go under Project in VS and "Project name" properties and under Debugging with Locals Windows Debugger and i supply the arguments under Command Arguments as -r test.txt , the program returns -1 not sure why.
will something like this work if( argv[1] == "-r") ?


No (for the reasons @jonnin gave you). Nearest possibilities are
if ( strcmp( argv[1], "-r" ) == 0 )
or, equivalently (as in my code sample):
if ( !strcmp( argv[1], "-r" ) )
or, converting one argument to a string:
if( string( argv[1] ) == "-r")

Sadly, you can't overload the == operator for two c-strings (but it didn't stop me trying!).
i tried running the program through the command prompt and it works fine.
but if i go under Project in VS and "Project name" properties and under Debugging with Locals Windows Debugger and i supply the arguments under Command Arguments as -r test.txt , the program returns -1 not sure why.


If you are referring to my code sample then it will return -1 for file not found (look at the code). So, you need to put your test.txt in whichever folder VS looks in (which I'm afraid I can't help you with).

If you can run it from the command line then perhaps you don't need VS ...
Last edited on
#include <string> and then:

1
2
// if( argv[1] == "-r") { /* ... */ }
if( argc > 1 && argv[1] == std::string( "-r" ) ) { /* ... */ }
It works with my VS 2015. Try printing all the commandline arguments:
1
2
3
4
for (int i = 0; i < argc; i++)
{
    cout << "argv[" << i << "] = " << argv[i] << "\n";
}
actually, [strcmp()] returns integers 0 or 1

No, it returns a number less than, equal to, or greater than zero, depending on whether the first argument compares less than, equal to, or greater than the second. In other words, -1, -25 and 974 are all legal return values.

The right way to handle strcmp is to always compare the result to zero.

I don't think it's part of the standard, but see if your environment supports getopt(). This is how UNIX has processed command lines for decades.
No, it returns a number less than, equal to, or greater than zero, depending on whether the first argument compares less than, equal to, or greater than the second.


My apologies - thank-you for the correction, @dhayden.

Fortunately, all the code samples are simply relying on whether the c-strings are equal (strcmp returns 0) or not equal (something other than 0).
Topic archived. No new replies allowed.