ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Distribution: MontaVista Linux Version 4.0.1, Professional Edition
Posts: 215
Rep:
Get a number between two numbers question
I need help with some c.
I have some code that opens a directory and reads in the names of files which are e.g. 0001, 0002, 0003 up to 9999.
I need to get all these numbers and then generate a new number that is not one of these numbers already.
here is my code to check the files in the directory
DIR *d;
struct dirent *dir;
int i = 0;
d = opendir( "//mydir//" );
if( d )
{
printf("Directory stream is now open\n");
while ((dir = readdir(d)) != NULL)
{
int num = atoi(dir->d_name);
printf("Filename=%s number=%d\n", dir->d_name, num);
if( num > 0 && num < 9999)//
{
//I need to add all my numbers to an array or list
// and then get a new number which is between 0 and 9999 //that does not include any of my added numbers
//Preferably the next logical number e.g. if i have 0,1,2,3 then i should get 4 as my next number
Create an int array n[10000] and set the value to 1 if the name is used, otherwise 0.
as in your example:
Code:
n[0] = 1;
n[1] = 1;
n[2] = 1;
n[3] = 1;
n[4] = 0;
and loop through searching for 0.
Another better and more difficult solution would be creating a singly linked list of unused names, which you could manage (add/remove members) dynamically.
Code:
struct slist
{
int value;
struct slist *bigger;
}
Last edited by Alien_Hominid; 02-01-2010 at 11:39 AM.
might be a more general solution. Then a tree search could be used to find the "next" gap which (for a large set of directory entries) would be more efficient than a sequential search. By using a dirent structure pointer (properly allocated, of course) in the list, the solution would be easily generalized to non-numeric file names.
You will read the whole directory every time the function is called.
Your error condition is to return "0000" as the file name. It might make the calling program more efficient if you set an error flag that could be checked.
If, instead, you followed the suggestion of Alien_hominid, you would initialize filenumbers to all zeros, and then, when a number is found, set filenumber[num]=1 Then your check would be for (j=num;j<10000 && filenum[j];++j) rather than a nested loop.
Last edited by PTrenholme; 02-01-2010 at 01:08 PM.
We are talking about finding a "missing number" between 1 and 9999. I'd take the easy array solution without thinking twice about it. I also consider it the superior approach because of the specifics of the task.
HarryBoy: Keep working on it, reread Alien_Hominid's initial suggestion.
That seems to imply creating the complete list first, also, assume you read number N, how do you remove the associated element from the list? A better question would be: what is the representation of the singly-linked list?
No, you read through directory listing. Assume 4 is missing, you add it to the list, then you find that 9 is missing, you add it to the list, etc, etc...
You get 4 -> 9 -> ...
While doubly linked list would be:
4 <-> 9 <->, which is unecessary because you always take lowest number first.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.