April 7th, 1999, 08:39 AM
Hi,
I have the following situation:
I have a program that allows manipulation of user details, ie: you can add/delete/modify/view a user. Each user when added has all of it's data stored
in a file in a dir. The filename is the user's ID. The program stores the users in an array in memory. I do NOT use linklist, because of other constraints.
The program, when started reads all the users from the dir, and then new users
can be added, existing ones can be manipulated. My problem is that, I get a random list of user ID's that was saved the last time. And the new ID's I have to allocate must be unique and not any of the existing ones. However they are not sorted. In some instances I could have,
1, 10, 112, 17, 4 ... etc.
These are not sorted, well actually, they are sorted on character ascending, since they are also filenames (thus strings).
Now normal adding happened, by having a counter that was incremented from 1, and each new user got his "unique" ID. If such an ID existed before, there would be two users with the same ID. I want some sort of algorithm or code sample that would go through the list of already "taken" ID's, and then like in the example above assign 2, to be the next available unique ID! I want a funtion, say int GetNextID() to do this. I cannot take the highest, in this case 112, and just add 1 to it for the new ID. Soon I'll run out of ID's!
16-bit unsigned max is 65535, and that's not enough. I will have to re-use deleted ID's.
Please help, and give some ideas.
PS: NO MFC, or classes, plain C code if you have samples.
Thanks in advance.
I have the following situation:
I have a program that allows manipulation of user details, ie: you can add/delete/modify/view a user. Each user when added has all of it's data stored
in a file in a dir. The filename is the user's ID. The program stores the users in an array in memory. I do NOT use linklist, because of other constraints.
The program, when started reads all the users from the dir, and then new users
can be added, existing ones can be manipulated. My problem is that, I get a random list of user ID's that was saved the last time. And the new ID's I have to allocate must be unique and not any of the existing ones. However they are not sorted. In some instances I could have,
1, 10, 112, 17, 4 ... etc.
These are not sorted, well actually, they are sorted on character ascending, since they are also filenames (thus strings).
Now normal adding happened, by having a counter that was incremented from 1, and each new user got his "unique" ID. If such an ID existed before, there would be two users with the same ID. I want some sort of algorithm or code sample that would go through the list of already "taken" ID's, and then like in the example above assign 2, to be the next available unique ID! I want a funtion, say int GetNextID() to do this. I cannot take the highest, in this case 112, and just add 1 to it for the new ID. Soon I'll run out of ID's!
16-bit unsigned max is 65535, and that's not enough. I will have to re-use deleted ID's.
Please help, and give some ideas.
PS: NO MFC, or classes, plain C code if you have samples.
Thanks in advance.