ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
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.
Introduction to Linux - A Hands on Guide
This guide was created as an overview of the Linux Operating System, geared toward new users as an exploration tour and getting started guide, with exercises at the end of each chapter.
For more advanced trainees it can be a desktop reference, and a collection of the base knowledge needed to proceed with system and network administration. This book contains many real life examples derived from the author's experience as a Linux system and network administrator, trainer and consultant. They hope these examples will help you to get a better understanding of the Linux system and that you feel encouraged to try out things on your own.
Click Here to receive this Complete Guide absolutely free.
Distribution: MontaVista Linux Version 4.0.1, Professional Edition
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
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
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 02: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?