LinuxQuestions.org
Help answer threads with 0 replies.
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 02-23-2006, 05:43 PM   #1
allomeen
Member
 
Registered: Dec 2005
Posts: 83

Rep: Reputation: 15
get files from a folder


I am trying to write a function in my program to give me information on a selected folder.
Is there anyway using c++ or c in Linux to get all the files listed in a folder.

Thanks in advance,
Alaa
 
Old 02-23-2006, 05:56 PM   #2
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
Quote:
Originally Posted by allomeen
Is there anyway using c++ or c in Linux to get all the files listed in a folder.
Yes. Example:
Code:
/* tls.c - Trivial ls command to list regular files only. 
 */
#include <stdio.h>
#include <dirent.h>
#include <unistd.h>
#include <sys/stat.h>

int main(int argc, char *argv[])
{
    /* String to hold directory name. */
    char *dirname;

    /* Pointers needed by functions for iterating directory entries. */
    DIR *dir;
    struct dirent *entry;

    /* Struct needed for determining info about an entry with stat(). */
    struct stat statinfo;

    /* Handle command line parameter (the directory to read) if given.. */
    if (argc > 2) {
        fprintf(stderr, "Usage: %s [<directory>]\n", argv[0]);
        return 1;
    } else if (argc == 2) {
        dirname = argv[1];
    } else {
        dirname = ".";  /* default to current working directory */
    }

    /* Open the directory */
    if ((dir = opendir(dirname)) == NULL) {
        fprintf(stderr, "ERROR: Could not open directory %s\n", dirname);
        return 1;
    }

    /* Change working dir to be able to read file information. */
    chdir(dirname);

    /* Loop through all entries in the directory. */
    while ((entry = readdir(dir)) != NULL) {
        /* List regular files only. */
        stat(entry->d_name, &statinfo);
        if (S_ISREG(statinfo.st_mode)) {
            puts(entry->d_name);  /* print the name */
        }
    }

    /* Finally close the directory stream. */
    closedir(dir);
    return 0;
}

Last edited by Hko; 02-23-2006 at 06:02 PM.
 
Old 02-23-2006, 06:02 PM   #3
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
Oops,.. double post.
Sorry. Please ignore this one.

Last edited by Hko; 02-23-2006 at 06:05 PM. Reason: Fixed indention
 
Old 02-23-2006, 06:06 PM   #4
allomeen
Member
 
Registered: Dec 2005
Posts: 83

Original Poster
Rep: Reputation: 15
Thanks so much, you saved me

Alaa
 
  


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
duplicate files in one folder! hornung Linux - Enterprise 1 01-13-2005 03:35 PM
IglooFTP-PRO refuses to upload files in folder when I upload folder eBopBob Linux - Software 0 08-27-2004 05:52 AM
Can't see files in a FAT32 Folder +joni Mandriva 4 05-10-2004 11:48 AM
Folder location of new files timsch75 Linux - Newbie 2 03-22-2004 10:02 AM
Saving files to other folder klmn1 Linux - Software 2 08-11-2003 12:28 PM

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

All times are GMT -5. The time now is 10:34 AM.

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