3

How would you use an if/then statement using argv[], upon the option/parameter entered?

For example, a.out -d 1 sample.txt versus a.out -e 1 sample.txt.

int main (int argc, char *argv[])
{
  ifstream infile(argv[3]);

  int c;
  int number = 0;
  int count = 0;

  while ( infile.good() ) {

            if (argv[1] == "-d")
            {

                 c = infile.get();

                 number = atoi(argv[2]);  

                  if (number == count)
                 {
                 cout.put(c);
                 count = 0;
                 }

                      else
                        count++;

            }       


           if (argv[1] == "-e")
           { 
              cout << "entered -e" << endl;   //testing code
           }


  }//end while

}//end main
Sergei Nikulov
  • 5,029
  • 23
  • 36
harman2012
  • 103
  • 3
  • 8

5 Answers5

3

You can't use the equality operator to compare C-style strings, you have to use std::strcmp:

if (std::strcmp(argv[1], "-d") == 0)

The reason behind that is that the == operator compares the pointers not what they point to.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
1

I hope you want to check the input parameter -d or -e, right? if that is the case please use strcmp()

if (!strcmp(argv[1],"-d")) {

            count++;
            printf("\ncount=%d",count);

        }       

       if (!strcmp(argv[1],"-e"))
       { 
          printf("entered -e");   //testing code
       }
1

The first error is in the very first line of main:

ifstream infile(argv[3]);

You cannot write that because there is no third argument. When you invoke your program like this:

a.out -d 1 < sample.txt

then the command line that the program sees looks like this:

argv[0] = "a.out"
argv[1] = "-d"
argv[2] = "1"

The < sample.txt, by contrast, is interpreted by the shell directly, and the file is streamed to the standard input of your program – and there’s nothing you can do to change that, inside your program.

As for the parsing itself, don’t do it inside the loop which reads the file, do it before and set appropriate flags.

For the actual parsing I’d suggest using a library to spare you a lot of pain. The standard Unix tool is getopt but that’s only got a C interface. There are several C++ libraries, amongst them Boost.Program_Options which is a tad too complex for my taste, though.

Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
  • Thanks everyone! I did have to change it to string compare, strcmp didn't work when I changed a few things. – harman2012 Nov 29 '12 at 06:53
1

The argc/argv comes from C and is rather cumbersome to use, so when more than basic argument passing is done, you can transform the arguments to a vector of strings and work in a C++ style:

#include <iostream>
#include <vector>
#include <algorithm>
#include <iterator>

main(int argc, char* argv[])
{
  std::vector<std::string> args;
  std::transform(argv+1, argv+argc, std::back_inserter(args), [](char* arg){ return std::string(arg); });

  if (args.at(1) == "-d") { std::cout << "Arg 1 = -d" << "\n"; }

  for (auto& arg: args) { std::cout << "arg: " << arg << "\n"; }
}

Still, you need to check that the argument is present. If this is a basic tool and it is acceptable that the tool aborts when the parameter is not present, then you can access args elements with args.at(x) instead of args[x]

Or check this SO question for an argument parser.

Community
  • 1
  • 1
stefaanv
  • 14,072
  • 2
  • 31
  • 53
0

char argv[] is an array of char * so

 if (string(argv[1]) == "-d")
SDEZero
  • 363
  • 2
  • 13