• C

UNIX C programming variable manipulation

UNIX korn shell
I have a Korn shell program I am writing where I would like to pass a variable
number to a C program.  This will get passed as a char variable but I would like
to convert it to a long

This is my call to the C program from Korn

GETCUSTIDAPP 12345

This will get passed into the main via:

int GETCUSTIDAPP(int argc, char **argv)

Where I set the variable passed in to a variable called

char custId[10]

Now, I need to pass in this custId to another function Get_Address and convert it to a long I'm having trouble with it understanding how to do it:

Here is what I have:
int Get_Address(char* customer) --> is this the right way to pass it in?
Also how do I convert this value to a long within the function?  I need to use this value in a sql call as a number later in the code.

I hope this made some sense.
farekatAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

evilrixSenior Software Engineer (Avast)Commented:
Use scanf to convert from a char[] to a long.
http://www.cplusplus.com/reference/clibrary/cstdio/scanf.html
Something like below...

long nCustId = 0;
scanf("%ld", custId);

Open in new window

0
ravenplCommented:
if (argc < 2)
{
 fprintf(stderr, "Need parameter");
 exit(1);
}

strncpy(custId, argv[1], 10);
custId[9] = 0; //good habit

long num = strtol(custId, NULL, 10);
0
evilrixSenior Software Engineer (Avast)Commented:
Sorry, my head is somewhere else... ignore above! I'll try again!

Use sscanf to convert from a char[] to a long.
http://www.cplusplus.com/reference/clibrary/cstdio/sscanf.html
Something like below...

long nCustId = 0;
sscanf(custId, "%ld", &nCustId);

Open in new window

0
jkrCommented:
There are several ways to convert strings to numbers, I'd prefer 'strtol()' because of the error checking. You can also use 'atoi()', but it has almost no error control. See also http://cplusplus.com/reference/clibrary/cstdlib/strtol.html and http://cplusplus.com/reference/clibrary/cstdlib/atoi.html
#include <stdio.h>
#include <stdlib.h>
 
int Get_Address(char* customer);
 
int main(int argc, char **argv) {
 
    char custId[10];
 
    if (argc != 2) {
 
        printf("Exactly one parameter expected\n");
 
        return -1;
    }
 
    strcpy(custId,argv[1]);
 
    Get_Address(custId);
 
    return 0;
}
 
int Get_Address(char* customer) {
 
    long l;
    char* cnv_end;
 
    l = strtol(customer,&cnv_end,10);
 
    if (*cnv_end != '\0') { // error - conversion found non-numeric characters
 
        printf("Error - not a number\n");
 
        return -1;
 
    }
 
    printf("ID: %d\n", l);
 
    return 42;
}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ravenplCommented:
jkr
> strcpy(custId,argv[1]);
is vulnerable to buffer overflow easily.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.