So I want through the most topics related to using strtol in respect of inputs, but didn't find the exact issue I look for.
Basically, I have to build some data structure in C (m-ary tree), and we receive the data of vertices from lines of the text file.
Now, for example row number 7 represents vertex number 5, and this row has to include only positive integers including zero, delimited by only one whitespace, these integers are actually the children of this row's vertex.
Now, I'm of course using strtol to get the integral values, and going through the next ones and receive them, but it doesn't cover all the possible scenarios the could appear.
The row can be invalid, for example "1.2.3" which is invalid, but my strtol will get the first value, 1, going to the next one, will consider it as 0, which is wrong.
One more issue that can appear is maybe the number is not integer but a double like - 2.5 - then again it will consider this as 2 and 0 where it has to be an error of invalid input.
So I thought to use strtok to split the integer fields by whitespace and then call on each one strtol, but again the row can be maybe 2.5 3.5 and strtol will treat it as 2 0 3 0 which is incorrect.
And I'm not sure it's so elegant to use both strtol and strtok.
Can you maybe explain me some other way to treat this issue well?
Assumptions: 1. the maximum length of a row is 1024 chars. 2. Every row represents a vertex and every value separated by a single whitespace. What we can't assume: 1. The values are not necessarily valid, We are required to validate that the value we have is an unsigned integer. For example, the input "1.2.3" is invalid. 2. Duplicate vertex in one line may appear and it is considered as an invalid input. The whole handling of reading from files etc. has to fit in Windows and Linux. In case of invalid input, we have to print to stderr "Invalid input\n" and return EXIT_FAILURE value.