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


 
Thread Tools Search this Thread
Top Forums Programming Need help in storing command line argument argv[2] to a variable of int type
# 1  
Old 12-12-2008
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...........SmilieSmilie


#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 < ARGCNT ) {
printf ("Invalid number of arguments.\n");
printf ("BASIL B.C. 2008.\n");
exit(1);
}

for (i=1;i<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;
}
# 2  
Old 12-12-2008
Code:
int iFilecount=0;
// later on in the code...
iFilecount=atoi(argv[2]);

P
# 3  
Old 12-12-2008
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.

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

# 4  
Old 12-12-2008
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. Smilie That won't work.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Storing awk command in a variable

I'm working on a script in which gives certain details in its output depending on user-specified options. So, what I'd like to do is something like: if then awkcmd='some_awk_command' else awkcmd='some_other_awk_command' fi Then, later in the script, we'd do something like: ... (5 Replies)
Discussion started by: treesloth
5 Replies

2. Shell Programming and Scripting

Storing command output in a variable and using cut/awk

Hi, My aim is to get the md5 hash of a file and store it in a variable. var1="md5sum file1" $var1 The above outputs fine but also contains the filename, so somthing like this 243ASsf25 file1 i just need to get the first part and put it into a variable. var1="md5sum file1"... (5 Replies)
Discussion started by: JustALol
5 Replies

3. Programming

Splitting string and storing in int

I have a string containing 2 integers separated by /, for example 12/8 or 8/6 am want to store the numbers in two integers. (3 Replies)
Discussion started by: kristinu
3 Replies

4. Shell Programming and Scripting

Pass command line argument to variable

Hi, I had written a shell script to pass command line argument to variable in a function. Here is my code: main if ; then .$1 echo $1 get_input_file else echo "input file $1 is not available" fi get_input_file() { FILE = "$1" echo $FILE } (10 Replies)
Discussion started by: Poonamol
10 Replies

5. Shell Programming and Scripting

Storing o/p of a command to a variable

Hi, I have a ftp script there I want to store the o/p of the below command: sftp -b <batch file> user@password cat <batch file> get /remote/file/path/remote_file_name.csv*.gz /local/path Now the problem is that when I fire this command. Then it gives o/p as: File... (7 Replies)
Discussion started by: dips_ag
7 Replies

6. Shell Programming and Scripting

Using ARGV, acepting text from command line

I want to be able to call in my file and make it do it's magic by basically giving it: FileNAME.pl searchTerm fileToSearch It runs, and gives me the answers I want, however, it gives me an error: Can't open GAATTC: No such file or directory at .//restriction_map_better.pl line 15... (2 Replies)
Discussion started by: silkiechicken
2 Replies

7. Shell Programming and Scripting

storing a command in a variable

how would i go about storing this command in a variable echo "$LINE" | awk -F"|" '{print $1"|"$2"|"$3"}' i have tried FOO = ${command up there} but receive the error FOO: not found aswell as a couple of other attempt but no luck (2 Replies)
Discussion started by: nookie
2 Replies

8. Shell Programming and Scripting

storing result of a command in variable

For whatever reason I cant seem to fix my syntax to do the following. I want to run a grep and count how many instances come up and store that number in a variable but I keep erroring out. Here's my code in bash: number=grep blah file.txt | wc -l (1 Reply)
Discussion started by: eltinator
1 Replies

9. Programming

Help - Cast converts default int return type

What does the warning message 724 "Cast converts default int return type to." tell me. I am new to C. (used it some in college). We are migrating our c-code to 10.2.0.3.0. The programs compiled clean. However there were several warning messages that kick out. The most prominent warning is: ... (5 Replies)
Discussion started by: rtgreen
5 Replies

10. Shell Programming and Scripting

Passing the command line argument in a variable

Hi, I am new to unix. Is their a way to pass the output of the line below to a variable var1. ls -1t | head -1. I am trying something like var1=ls -1t | head -1, but I get error. Situation is: I get file everyday through FTP in my unix box. I have to write a script that picks up first... (1 Reply)
Discussion started by: rkumar28
1 Replies
Login or Register to Ask a Question