W
William Payne
Hello, I am in the process of converting a C++ program to a C program. The
user of the program is supposed to supply an integer on the command line and
in the C++ version of the program I was using something called stringstreams
to do the conversion. Here's my C version, can I leave it as it is or does
it need to be robustified or changed in any manner, regarding error
checking?
char* endptr;
errno = 0;
unsigned long port_number = strtol(argv[2], &endptr, 0);
if(strcmp("", endptr) != 0)
{
fprintf(stderr, "Couldn't convert %s to a valid port number.\n",
argv[2]);
return EXIT_FAILURE;
}
if(errno == ERANGE)
{
fprintf(stderr,
"Value %s is too big or small to fit in an unsigned long.\n",
argv[2]);
return EXIT_FAILURE;
}
Thanks for any replies!
/ William Payne
user of the program is supposed to supply an integer on the command line and
in the C++ version of the program I was using something called stringstreams
to do the conversion. Here's my C version, can I leave it as it is or does
it need to be robustified or changed in any manner, regarding error
checking?
char* endptr;
errno = 0;
unsigned long port_number = strtol(argv[2], &endptr, 0);
if(strcmp("", endptr) != 0)
{
fprintf(stderr, "Couldn't convert %s to a valid port number.\n",
argv[2]);
return EXIT_FAILURE;
}
if(errno == ERANGE)
{
fprintf(stderr,
"Value %s is too big or small to fit in an unsigned long.\n",
argv[2]);
return EXIT_FAILURE;
}
Thanks for any replies!
/ William Payne