readdir() gives "." and ".." and doesn't exclude some folders
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.
readdir() gives "." and ".." and doesn't exclude some folders
I was trying to make an interactive directory menu.
But in the directory where he has to read, there are a few directories that i don't want my menu to show and the "." and ".." still shows to...
I just changed all my specific names so you can modify it easier...
please help me, and here is my code..
Code:
#include <stdio.h>
#include <errno.h>
#include <sys/types.h>
#include <dirent.h>
#define dir getenv("HOME")
main()
{
DIR *dip;
struct dirent *dit;
int i = 1;
int n = 0;
char list_dir[20];
char* list_var[8];
dip = opendir(dir);
if (dip == NULL)
{
perror("opendir");
return(CODE);
}
while (( dit = readdir(dip)) != NULL)
{
start:
sprintf(list_dir,"%s",dit->d_name);
if (list_dir = "bad_dir"){goto start;}
if(list_dir == "."){goto start;}
if(list_dir == ".."){goto start;}
printf("\t\t\t(%d) %s\n",i,list_dir);
list_var[n] = list_dir;
i++;
n++;
}
if (closedir(dip) == -1)
{
perror("closedir");
return 0;
}
}
at the end there was a switch for the choice list and list_var[n] was a variable that takes the the name of the dir chosen, for further use...
But the problem here...
1 -> it still doesn't exclude the "." ".." and "bad_dir" from my list
2 -> my list_var always give the last dir in my case always the ".." :-(
Please give me some help i've changed this code 100 times but can't find a good solution.
I was trying to make an interactive directory menu.
But in the directory where he has to read, there are a few directories that i don't want my menu to show and the "." and ".." still shows to...
I just changed all my specific names so you can modify it easier...
please help me, and here is my code..
Code:
#include <stdio.h>
#include <errno.h>
#include <sys/types.h>
#include <dirent.h>
#define dir getenv("HOME")
main()
{
DIR *dip;
struct dirent *dit;
int i = 1;
int n = 0;
char list_dir[20];
char* list_var[8];
dip = opendir(dir);
if (dip == NULL)
{
perror("opendir");
return(CODE);
}
while (( dit = readdir(dip)) != NULL)
{
start:
sprintf(list_dir,"%s",dit->d_name);
if (list_dir = "bad_dir"){goto start;}
if(list_dir == "."){goto start;}
if(list_dir == ".."){goto start;}
printf("\t\t\t(%d) %s\n",i,list_dir);
list_var[n] = list_dir;
i++;
n++;
}
if (closedir(dip) == -1)
{
perror("closedir");
return 0;
}
}
at the end there was a switch for the choice list and list_var[n] was a variable that takes the the name of the dir chosen, for further use...
But the problem here...
1 -> it still doesn't exclude the "." ".." and "bad_dir" from my list
2 -> my list_var always give the last dir in my case always the ".." :-(
Please give me some help i've changed this code 100 times but can't find a good solution.
And why do you think your code will ever get out of, say,
Code:
start:
...
if(list_dir == "."){goto start;}
loop ? I.e. what in your opinion will make this loop terminate ?
And why do you think your code will ever get out of, say,
Code:
start:
...
if(list_dir == "."){goto start;}
loop ? I.e. what in your opinion will make this loop terminate ?
Cause on the ... there was a code where he would take another dir of the list. but it just still printed the "." ,So i'm clueless what really happened in this bad code...
meanwhile i found a different method, cause someone told me you can only compare int's with ==
So i used
Anyway, frankly, I do not understand what this thread is all about. It's universally known that one can't rely on order of items returned by 'readdir', so typically a list of items is created in an elastic array by calling 'readdir' in a loop, and the loop should be fast for more consistent results; then the items in that elastic array is manipulated.
It's universally known that one can't rely on order of items returned by 'readdir'...
Not to be overly literal, but obviously it's not universally known. The OP didn't seem to know; my mom has no idea about this, nor does my dad, nor most of the people I work with.
Being more serious, I've seen plenty of programmers in Perl be confused about the behavior of readdir. Apparently, people aren't born knowing everything, they don't always read all the documentation, they don't understand everything that they do read and they don't remember everything that they read and understand.
Last edited by Telemachos; 08-13-2009 at 12:48 PM.
Anyway, frankly, I do not understand what this thread is all about. It's universally known that one can't rely on order of items returned by 'readdir', so typically a list of items is created in an elastic array by calling 'readdir' in a loop, and the loop should be fast for more consistent results; then the items in that elastic array is manipulated.
just try to read the title and the content of my first post...
Frankly the thing that i don't understand, but i'm not an expert...
Computers aren't able of thinking so they will do everything in a consequently... this means if we say give the alphabet it will always give it from A to Z and nothing else...
Why readdir goes from Z to A with certain "." and ".." in it that don't belong there... I don't understand that either...
Beside i wanted to eliminate some other dir's in my list that i can't move out of the main dir...
thanks anyway for your enlightning answer and the solution of my problem...
just try to read the title and the content of my first post...
Frankly the thing that i don't understand, but i'm not an expert...
Computers aren't able of thinking so they will do everything in a consequently... this means if we say give the alphabet it will always give it from A to Z and nothing else...
Why readdir goes from Z to A with certain "." and ".." in it that don't belong there... I don't understand that either...
Beside i wanted to eliminate some other dir's in my list that i can't move out of the main dir...
thanks anyway for your enlightning answer and the solution of my problem...
If any other process for whatever reason creates and deletes an entry in the directory under question, the order of items returned by 'readdir' will most likely change - this was my point.
If any other process for whatever reason creates and deletes an entry in the directory under question, the order of items returned by 'readdir' will most likely change - this was my point.
I don't mind anymore that the order changes... my point was that it would appear in a menu numbered from 1 to 10... it prints fine now, but i cannot seem to pipe it through my switch, here's the thing that i tried out...
Code:
struct cas_name
{
char c1[16];
char c2[16];
char c3[16];
char c4[16];
char c5[16];
char c6[16];
char c7[16];
char c8[16];
char c9[16];
} casi_names1;
main()
{
DIR *dip;
struct dirent *dit;
int i = 1;
char list_dir[20];
dip = opendir(dir);
if (dip == NULL)
{
perror("opendir");
return(CODE);
}
while (( dit = readdir(dip)) != NULL)
{
start:
sprintf(list_dir,"%s",dit->d_name);
if (strcmp(list_dir, "bad_dir")==0){continue;}
if(strcmp(list_dir, ".")==0){continue;}
if(strcmp(list_dir, "..")==0){continue;}
else
{
printf("\t\t\t(%d) %s\n",i,list_dir);
strcpy(casi_names1.c[i], list_dir);
i++;
}
}
if (closedir(dip) == -1)
{
perror("closedir");
return 0;
}
}
/* here comes the switch statement where i defines every nmbr with the a var of the struct casi_names.c[1 to 10] pipe it through the next function.
But here is my problem: (one i understand but didn't find a solution for)
1 -> when it runs it will give me the error cas_name has no member named c
-> -> do you know how to modify this so i get this dir names in mys struct???
Anyway, frankly, I do not understand what this thread is all about. It's universally known that one can't rely on order of items returned by 'readdir', so typically a list of items is created in an elastic array by calling 'readdir' in a loop, and the loop should be fast for more consistent results; then the items in that elastic array is manipulated.
Not only is that not universally known, it's totally unnecessary! man 3 scandir for information on what one does to get sorted directory entries.
Quote:
Originally Posted by Tordne
But here is my problem: (one i understand but didn't find a solution for)
1 -> when it runs it will give me the error cas_name has no member named c
-> -> do you know how to modify this so i get this dir names in mys struct???
Actually, I don't think you understand the problem (hence why you don't know how to fix the compiler error).
You might want to investigate multi-dimensional arrays, their indexing, and what a structure should be used for.
Not only is that not universally known, it's totally unnecessary! man 3 scandir for information on what one does to get sorted directory entries.
Actually, I don't think you understand the problem (hence why you don't know how to fix the compiler error).
You might want to investigate multi-dimensional arrays, their indexing, and what a structure should be used for.
thanks and certainly fantas,
this was the thing i needed,
I'm sorry, but every thing i know i learned myself, and everything took a looong loooong time...
i wrote a few shell progs, but my last one got so complicated and not suitable for shell ( bash, awk, grep,...)
I'm sorry if you think i ask stupid questions, but the book i learned from is a bit out of date (before 1999) and isn't well explained.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.