Need help in storing command line argument argv[2] to a variable of int type

The following program takes two command line arguments.

I want the second argument (fileCount) to be stored/printed as a int value. I tried my best to typecast the char to int (check the printf statement at last) but is not working...the output is some junk value.

This program is in its beginning stage....this program is expected to take 2 list of strings along with the list count from files generated by a shell script and compare them both for equality ( & also checking for any extra or missing items using C++ hash functions (dictionary search) ) so the input is supposed to be the file name and list item count...the shell script is ready but...i am stuck up with the typecast for converting char to int...kindly help....& thanks in advance...........:):confused:

#include <iostream>
using namespace std;
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
//#include <search.h>

#define SIZ 100 // Dictionary size
#define ARGCNT 3 // Argument count

//------------------------------------------------------------------------------

// GLOBAL

//char *str[SIZ];
FILE *fp;
char *fileCount;
char *fileList;

//------------------------------------------------------------------------------

// FILE PROCESSOR

//------------------------------------------------------------------------------

// MAIN

main( int argc,
char *argv[])
{
int i;

    if \( argc &lt; ARGCNT \) \{
            printf \("Invalid number of arguments.\\n"\);
            printf \("BASIL B.C. 2008.\\n"\);
            exit\(1\);
    \}

    for \(i=1;i&lt;argc;i\+\+\) \{

            switch \(i\) \{

            case 1:
                    fileList = argv[i];
                    break;

            case 2:
                    fileCount = argv[i];
                    break;

             default:
                     printf \("Not implemented yet !.\\n"\);
                     printf \("BASIL B.C. 2008.\\n"\);
             \}

     \}
             printf\("%s  %d\\n",fileList,*\(\(int *\) fileCount\)\);

     return 0;

}

int iFilecount=0;
// later on in the code...
iFilecount=atoi(argv[2]);

P

While there's nothing wrong with jim's answer, and at the risk of confusing the issue, there are alternatives:

In C, I usually prefer the strtol function because it allows checking for errors.

In C++ (since I guess that's what you are using). You can use a stringstream object, e.g.

  std::istringstream is;
  is.str( argv[2] );
  int fileCount;
  if( !( is >> fileCount ) )
  {
    // handle error 
  }

If the OP is using C++ he needs to get the include files straightened out as well.
It was not clear to me what language was being used. Where I live people mix English + Spanish = Spanglish. So I guess the code is "C+" - a hybrid. :slight_smile: That won't work.