LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-12-2009, 03:14 PM   #1
Tordne
Member
 
Registered: Mar 2008
Location: London,UK
Distribution: Ubuntu
Posts: 37

Rep: Reputation: 15
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.
 
Old 08-12-2009, 03:25 PM   #2
raconteur
Member
 
Registered: Dec 2007
Location: Slightly left of center
Distribution: slackware
Posts: 276
Blog Entries: 2

Rep: Reputation: 44
I suggest using "continue" instead of "goto".
I'm not one of those folks who claim goto is inherently evil (but I do play one on tv).
 
Old 08-12-2009, 03:33 PM   #3
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by Tordne View Post
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 ?
 
Old 08-13-2009, 07:09 AM   #4
Tordne
Member
 
Registered: Mar 2008
Location: London,UK
Distribution: Ubuntu
Posts: 37

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by Sergei Steshenko View Post
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
Code:
if(strcmp(list_dir, bad_dir)==0){continue;}
And this works for me,...
 
Old 08-13-2009, 07:18 AM   #5
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
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.
 
Old 08-13-2009, 09:13 AM   #6
fantas
Member
 
Registered: Jun 2007
Location: Bavaria
Distribution: slackware, xubuntu
Posts: 143

Rep: Reputation: 22
Code:
if (list_dir = "bad_dir"){goto start;}
Typo ?
 
Old 08-13-2009, 12:08 PM   #7
Telemachos
Member
 
Registered: May 2007
Distribution: Debian
Posts: 754

Rep: Reputation: 60
Quote:
Originally Posted by Sergei Steshenko View Post
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.
 
Old 08-13-2009, 12:20 PM   #8
Tordne
Member
 
Registered: Mar 2008
Location: London,UK
Distribution: Ubuntu
Posts: 37

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by Sergei Steshenko View Post
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...
 
Old 08-13-2009, 12:49 PM   #9
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by Tordne View Post
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.
 
Old 08-13-2009, 02:10 PM   #10
Tordne
Member
 
Registered: Mar 2008
Location: London,UK
Distribution: Ubuntu
Posts: 37

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by Sergei Steshenko View Post
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???
 
Old 08-13-2009, 02:38 PM   #11
fantas
Member
 
Registered: Jun 2007
Location: Bavaria
Distribution: slackware, xubuntu
Posts: 143

Rep: Reputation: 22
instead of
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;
use
Code:
struct cas_name
{
  char c[9][16];
} casi_names1;
then you can adress the arrays by 'casi_names1.c[i]'
 
Old 08-13-2009, 02:40 PM   #12
orgcandman
Member
 
Registered: May 2002
Location: new hampshire
Distribution: Fedora, RHEL
Posts: 600

Rep: Reputation: 110Reputation: 110
Quote:
Originally Posted by Sergei Steshenko View Post
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.
 
Old 08-13-2009, 03:01 PM   #13
Tordne
Member
 
Registered: Mar 2008
Location: London,UK
Distribution: Ubuntu
Posts: 37

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by orgcandman View Post
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.

thanks anyway...
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How might I restore kmail folders/mail/settings from a "badly" saved "home"? deh6 Linux - Software 5 03-08-2008 09:25 PM
How do you create shortcuts to "folders" and "binaries" in GNOME? ebenh Linux - Desktop 5 01-21-2008 05:28 AM
Standard commands give "-bash: open: command not found" even in "su -" and "su root" mibo12 Linux - General 4 11-11-2007 10:18 PM
Displaying "Subscribed Threads" -> new folder "All Folders" JZL240I-U LQ Suggestions & Feedback 5 06-15-2007 06:36 AM
LXer: Displaying "MyComputer", "Trash", "Network Servers" Icons On A GNOME Desktop LXer Syndicated Linux News 0 04-02-2007 08:31 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:40 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration