farekat
asked on
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.
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.
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);
{
fprintf(stderr, "Need parameter");
exit(1);
}
strncpy(custId, argv[1], 10);
custId[9] = 0; //good habit
long num = strtol(custId, NULL, 10);
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...
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);
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
jkr
> strcpy(custId,argv[1]);
is vulnerable to buffer overflow easily.
> strcpy(custId,argv[1]);
is vulnerable to buffer overflow easily.
http://www.cplusplus.com/reference/clibrary/cstdio/scanf.html
Something like below...
Open in new window