LinuxQuestions.org
Review your favorite Linux distribution.
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 05-24-2002, 10:11 PM   #1
tristan_vdv
LQ Newbie
 
Registered: Nov 2001
Location: Australia
Distribution: RH 7.2
Posts: 17

Rep: Reputation: 0
Enumerating Files in a directory in C


Hi,
here is another file system question:
In Linux, how do you get a list of the files in a directory in C? In windows you used the findfirstfile etc api, i have no idea how to do it in linux.

Thanks in advance!
Tristan
 
Old 05-24-2002, 11:32 PM   #2
tyler_durden
Member
 
Registered: May 2001
Posts: 125

Rep: Reputation: 15
here, check this out

http://www.cs.cf.ac.uk/Dave/C/node20...00000000000000
 
Old 05-25-2002, 07:43 PM   #3
crabboy
Senior Member
 
Registered: Feb 2001
Location: Atlanta, GA
Distribution: Slackware
Posts: 1,821

Rep: Reputation: 121Reputation: 121
Code:
#include <stdio.h>
#include <dirent.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/param.h>

int main (int argc, char * argv[])
{
    struct stat stDirInfo;
    struct dirent * stFiles;
    DIR * stDirIn;
    char szFullName[MAXPATHLEN];
    char szDirectory[MAXPATHLEN];
    struct stat stFileInfo;
    short int mode;

    if ( argc == 2 )
        strncpy( szDirectory, argv[1], MAXPATHLEN - 1 );
    else
        exit(-1);

    if (lstat( szDirectory, &stDirInfo) < 0)
    {
        perror (szDirectory);
        return;
    }
    if (!S_ISDIR(stDirInfo.st_mode))
        return;
    if ((stDirIn = opendir( szDirectory)) == NULL)
    {
        perror( szDirectory );
        return;
    }
    while (( stFiles = readdir(stDirIn)) != NULL)
    {
        sprintf(szFullName, "%s/%s", szDirectory, stFiles -> d_name );

        if (lstat(szFullName, &stFileInfo) < 0)
           perror ( szFullName );

        /* is the file a directory? */
        if (S_ISDIR(stFileInfo.st_mode))
        {
            printf( "Directory: %s\n", szFullName );
        }
        else
        {
            printf( "Filename: %s\n", szFullName );
        }
/*
        if ( S_ISLNK( stFileInfo.st_mode ))
           printf("is link\n");
*/

    }  // end while
    closedir(stDirIn);
    return;
}  // end main
 
Old 05-25-2002, 10:29 PM   #4
tristan_vdv
LQ Newbie
 
Registered: Nov 2001
Location: Australia
Distribution: RH 7.2
Posts: 17

Original Poster
Rep: Reputation: 0
Thanks guys.
Is there much difference between using scandir or opendir and readdir? ie a big speed or compatability difference?
 
  


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
Autozipping files from 1 directory & dropping them in other directory ??? amitsharma_26 Linux - Networking 5 10-22-2005 06:09 AM
copy files from directory to directory without subfile ALInux Linux - General 2 06-03-2005 11:51 AM
Automatically Copying files from the ftp directory into the html directory swatward Linux - General 3 04-17-2005 10:55 PM
home directory files gone, hidden files remain Grasshopper Linux - Security 12 04-10-2005 08:23 PM
Enumerating shares with Samba computergeek84 Linux - Software 1 11-02-2003 12:54 PM

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

All times are GMT -5. The time now is 09:20 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