LinuxQuestions.org
Register a domain and help support LQ
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
 
LinkBack Search this Thread
Old 10-10-2003, 06:54 PM   #1
sutho1
LQ Newbie
 
Registered: Sep 2003
Location: Townsville Australia
Distribution: redhat 9
Posts: 10

Rep: Reputation: 0
using readdir to display files


Hi all
I am wondering if anyone can help me with a programming problem I am having. I have written a program that mimics the command ls -1F and it will open the current working directory and then display all the files and sub directories located there. What it does not do however is display the symbol after the file name eg a * for an executable, a / for a directory, a @ for a symbolic link etc. Can anyone explain to me how these symbols could be made to display. Is there a function in either dirent.h or stat.h that can do this?

Here is my code so far.....




#include <iostream>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>

using namespace std;

int main(int argc, char *argv[])
{

struct dirent *dir;
char *cwd;
DIR *open;
char *ptr;


if(argc > 1)
cout<<"ls -1F ";
for (int i=1; i < argc; i++)
cout <<argv[i];
cout<<endl;
if(argc < 2)
{
cwd = getcwd(ptr,20);
open = opendir(cwd);
while((dir = readdir(open)) != NULL)
cout<<dir->d_name<<endl;

}

exit(0);
}



Thanks
Sutho
 
Old 10-10-2003, 08:38 PM   #2
mr_segfault
Member
 
Registered: Oct 2003
Location: Australia
Distribution: Redhat 9
Posts: 95

Rep: Reputation: 15
There sure is. Do a:

man -S2 stat

Note the S is uppercase..

this will allow you to stat each file, then determin if it is a link/regularfile/directory etc..

There might be an easier/quicker (as in cheaper) method but I can't think of one at the moment..

Cheers..
 
Old 10-10-2003, 09:09 PM   #3
SaTaN
Member
 
Registered: Aug 2003
Location: Suprisingly in Heaven
Posts: 223

Rep: Reputation: 32
I suppose this should make things clear...
I haven't got a linux machine to check it . But I think that this is the way you do it.

Code:
#include <dirent.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
int main(int argc,char **argv)
{
        int num,i;
        struct dirent **name_list;
        struct stat BUFFER;
        //The path of the directory to be listed should be given as cmd line argument
        num=scandir(argv[1],&name_list,0,alphasort);
        for(i=0;i<num;i++)
        {
                stat(name_list[i]->d_name,&BUFFER);
                //To check if it is a dir or not
                if(S_ISDIR(BUFFER.st_mode))
                {
                                printf("%s --- directory \n",name_list[i]->d_name);
                }
        }
}
P.S :- Is your program working fiine ??? I think there is an error with the getcwd thing...Maybe I am wrong about your program
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
about readdir r_213 Programming 1 01-16-2005 11:26 PM
Regarding readdir r_213 Linux - Networking 2 01-15-2005 07:56 AM
readdir then read the files jupe Programming 2 07-13-2004 10:04 AM
after readdir how can I open all the files jupe Programming 1 07-13-2004 09:00 AM
Opening a directory WITHOUT using 'opendir' and 'readdir' skywalker27182 Programming 2 11-16-2003 01:32 PM


All times are GMT -5. The time now is 05:45 AM.

Main Menu
 
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
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration