473,288 Members | 1,771 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,288 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 17208
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
626 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...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...

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.