473,395 Members | 1,383 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

C Validating >=1 numeric argv[]

15 Byte
I have a simple utility expecting three command-line arguments:
./myutil -k 123 45

argv[1] is a text flag of some sort: memcmp() against allowed flags; works!

argv[2] and argv[3] need to sit in unsigned 32-bit integers and are required to be >= 1.

For now I'm using:
strstr() to look for "-" ...and assuming absence of "-" means +ve number-as-string.
atoi() to get assumed +ve number into unsigned thisuint, thatuint
Then checking thisuint >= 1, thatuint >=1
It works for the test cases I've tried, but it seems mmmmmm clumsy.

Is there a better way?
As stated, this is a "simple utility": 1000 lines of validation code is the wrong choice.

Chris
Aug 12 '22 #1
5 17217
Luuk
1,047 Expert 1GB
Do you really need 1000 lines of code to do this validation in C? I do not have enough experience with C, but that seems too much. Maybe you can post the code that you wrote to get tips on how to improve the code.
Aug 14 '22 #2
Chris3020
15 Byte
@Luuk

I admit to being crazy, but I'm not quite crazy enough to devote 1000 lines of code to input validation for an integer!
Was just looking for a neat way that didn't involve a huge (1000 lines...) library designed to handle every nit-picky facet of argv[] parsing.

Since posting, I remembered abs() ...doh!

Input validation is now simple:
Expand|Select|Wrap|Line Numbers
  1.   unsigned this = abs(atoi(argv[2]));
  2.   unsigned that = abs(atoi(argv[3]));
  3.   if (this == 0 || that == 0){
  4.     puts("\n USAGE both this & that must be > 0\n exit...");
  5.     exit(EXIT_FAILURE);
  6.   }
Chris
Aug 17 '22 #3
Luuk
1,047 Expert 1GB
When entering `-12345678901234567890` as parameter, te value `-2147483648` is returned... 😢😉
Aug 19 '22 #4
dev7060
636 Expert 512MB
I have a simple utility expecting three command-line arguments:
./myutil -k 123 45

argv[1] is a text flag of some sort: memcmp() against allowed flags; works!

argv[2] and argv[3] need to sit in unsigned 32-bit integers and are required to be >= 1.

For now I'm using:
strstr() to look for "-" ...and assuming absence of "-" means +ve number-as-string.
atoi() to get assumed +ve number into unsigned thisuint, thatuint
Then checking thisuint >= 1, thatuint >=1
It works for the test cases I've tried, but it seems mmmmmm clumsy.

Is there a better way?
As stated, this is a "simple utility": 1000 lines of validation code is the wrong choice.
What specific validations are you looking for and why? How have you tested user input before? Who is the target user? There could be many things that may not go right. Using the primitive way is probably reasonable according to the use case like dealing with simple use cases (very few known flags, assumption that valid args are present in the correct order, format, sequence, type, range, count, etc). If it gets more complex than that or if the aim is to have a robust code and add flexibility and make sure everything is handled the right way, using a library would probably be a better choice. Having a well-tested library to take care of that stuff may allow to better focus on the mainstream part of the code.

From the provided context, I guess a few nested if-else blocks may be enough. Maybe show the complete validation part of the code for optimization.
Aug 19 '22 #5
Chris3020
15 Byte
...sorry for radio-silence: some stuff came up in the real-world.

Turned out that I was the only user for the C-version of the utility!
A friend asked for something to calculate the product of the first N prime-numbers after some number X.
...run-times of a few micro-seconds did not overcome his dislike of the command-line.
Once the algorithm was ironed out in C it got translated into VBA! He's happier with that.

@Luuk: thanks for the fun overflow illustration. When I have a moment I'll check what happens when an unsigned int overflows.
@dev7060: yes, for anything more serious I will try to find a decent library. It hasn't come up yet: my teach-myself-C exercise is a rework of something I did in Perl a year or two ago (too slow AND a lousy algorithm). C-version runs with hard-coded parameters for now. Three more modules to complete ...I hope by 27 December to mark the 1st anniversary of trying to learn C.

Chris
Nov 23 '22 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Johannes Nix |Johannes.Nix | last post by:
Hi, I have a tricky problem with Numeric. Some time ago, I have generated a huge and complex data structure, and stored it using the cPickle module. Now I want to evaluate it quickly again on a...
3
by: Mark | last post by:
Hi, Im trying to validate a form, all the validating works apart from one field. This particular field must consist of the first 2 characters as letters, & the following 5 as numbers. And if it...
4
by: Eric | last post by:
Is there a way to cancel the validating event on the closing event? I have 2 textboxes that I use the validating event to check for numeric data. If I try to close the form without putting a...
16
by: Keith | last post by:
Am I crazy - to be shocked that there is no Numeric, Alpha, and AlphaNumeric Property in on the Textbox control I searched and searched - and found other people's code - but they don't work...
12
by: patang | last post by:
I have written the following code which allows user to enter only numbers and only one decimal point. This works fine. However, I want that user shouldn't enter a third number after decimal...
0
by: Wayne Phipps | last post by:
Hi There, I want to be able to validate data entered by a user onto a form. Different fields will have different validation requirements. As an example, a text box may only allow numeric,...
5
by: Reny | last post by:
can any one tell how can i restrict my user to type just numeric character in the textbox.I am using VS.NET 2003 (VB.NET)
3
by: TheSteph | last post by:
Hi Experts ! I have a Winform Program in C# / .NET 2.0 I would like to ensure that a value in a TextBox is a valid Int32 when user get out of it (TextBox loose focus)
6
by: Richard | last post by:
I'm validating a date and time string which must be EXACTLY of the format yy-mm-dd hh:mm:ss and extracting the six numeric values using sscanf. I'm using the format string "%2u-%2u-%2u...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.