C++ Learning Community Forum
September 09, 2010, 08:14:16 AM *
Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
News: Hello. Smiley
 
   Home   Help Search Login Register  
Pages: [1]
  Print  
Author Topic: how to list files in a folder ?  (Read 640 times)
akira300
Nerd
****
Posts: 76



View Profile
« on: March 21, 2007, 11:10:34 AM »

how to list files in a folder Huh any1 an idea ?
Logged

\q|ô_ô|p/
C-Man
Does anyone even read this ?
Global Moderator
Dr. of C++ology
*****
Posts: 988



View Profile WWW
« Reply #1 on: March 21, 2007, 12:24:42 PM »

dirent.h my friend Smiley
Logged

dr voodoo
Global Moderator
rand()%title;
*****
Posts: 214


View Profile
« Reply #2 on: March 21, 2007, 03:49:24 PM »

Using boost you could do it like the following
Code
GeSHi (cpp):
#include <boost/filesystem/operations.hpp>
#include <boost/filesystem/path.hpp>
 
namespace fs = boost::filesystem;
 
int main(int argc, char*argv[]){
 for(int arg=0; arg<argc; ++arg){
   std::cout<<"Files in \""<<argv[arg]<<"\""<<std::endl;
   fs::path dir(argv[arg], fs::native);
   for(fs::directory_iterator file(dir), end; file!=end; ++file)
     std::cout<<file.native_file_string()<<std::endl;
 }
}
Created by GeSHI 1.0.7.18
Logged
zaqufant
Farmer Brown
Dr. of C++ology
****
Posts: 963


Harder, better, faster, stronger.


View Profile
« Reply #3 on: March 22, 2007, 01:24:48 AM »

Um, C-man, could you tell us a place were we can learn about dirent.h?
Logged
draco
Geek
*****
Posts: 137


View Profile
« Reply #4 on: March 22, 2007, 01:32:24 AM »

Code:
READDIR(3)    Linux Programmer's Manual     READDIR(3)



NAME
       readdir - read a directory

SYNOPSIS
       #include <sys/types.h>

       #include <dirent.h>

       struct dirent *readdir(DIR *dir);

DESCRIPTION
       The  readdir()  function returns a pointer to a dirent structure repre-
       senting the next directory entry in the directory stream pointed to  by
       dir.   It  returns  NULL on  reaching  the  end-of-file or if an error
       occurred.

       On Linux, the dirent structure is defined as follows:

struct dirent {
     ino_t     d_ino; /* inode number */
     off_t     d_off; /* offset to the next dirent */
     unsigned short d_reclen; /* length of this record */
     unsigned char  d_type; /* type of file */
     char     d_name[256]; /* filename */
};

       According to POSIX, the dirent structure contains a field char d_name[]
       of  unspecified size,  with  at most NAME_MAX characters preceding the
       terminating null byte.  POSIX.1-2001 also  documents  the  field ino_t
       d_ino as an XSI extension.  Use of other fields will harm the portabil-
       ity of your programs.

       The data returned by readdir() may be overwritten by  subsequent calls
       to readdir() for the same directory stream.

RETURN VALUE
       The readdir() function returns a pointer to a dirent structure, or NULL
       if an error occurs or end-of-file is reached.  On error, errno  is  set
       appropriately.

ERRORS
       EBADF  Invalid directory stream descriptor dir.

CONFORMING TO
       SVr4, 4.3BSD, POSIX.1-2001

SEE ALSO
       read(2), closedir(3), dirfd(3), ftw(3), opendir(3), rewinddir(3), scan-
       dir(3), seekdir(3), telldir(3)



  1996-04-22     READDIR(3)

Code:
OPENDIR(3)    Linux Programmer's Manual     OPENDIR(3)



NAME
       opendir - open a directory

SYNOPSIS
       #include <sys/types.h>
       #include <dirent.h>

       DIR *opendir(const char *name);

DESCRIPTION
       The  opendir()  function opens a directory stream corresponding to the
       directory name, and returns a pointer to the  directory stream.   The
       stream is positioned at the first entry in the directory.

RETURN VALUE
       The  opendir()  function returns a pointer to the directory stream.  On
       error, NULL is returned, and errno is set appropriately.

ERRORS
       EACCES Permission denied.

       EMFILE Too many file descriptors in use by process.

       ENFILE Too many files are currently open in the system.

       ENOENT Directory does not exist, or name is an empty string.

       ENOMEM Insufficient memory to complete the operation.

       ENOTDIR
      name is not a directory.

NOTES
       The underlying file descriptor of the directory stream can be  obtained
       using dirfd(3).

CONFORMING TO
       SVr4, 4.3BSD, POSIX.1-2001.

SEE ALSO
       open(2), closedir(3),  dirfd(3), readdir(3), rewinddir(3), scandir(3),
       seekdir(3), telldir(3)



  1995-06-11     OPENDIR(3)

And some code:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <dirent.h>

int main()
{
DIR *dir;
dirent *item;

dir = opendir("/home/draco/");

if ( dir == NULL )
{
printf("Error opening directory!\n");
                exit(1);
}

while ( (item = readdir(dir)) != NULL )
{
printf(item->d_name);
printf("\n");
}

        closedir(dir);
       
return 0;
}
« Last Edit: March 22, 2007, 02:16:52 AM by draco » Logged
ih8censorship
Megalomaniac!!!
Administrator
C++ guru
*****
Posts: 1241



View Profile
« Reply #5 on: March 22, 2007, 04:38:04 AM »

if your only concerned about doing it on windows, you could use the FindFirstFile and FindNextFile functions included with windows.h (or something that it includes anyway)
Logged

PC==perfect_companion

Knowledge cannot come packaged and predigested; it must be chewed over carefully before swallowed.

What have you tried?
adeyblue
Dr. of C++ology
****
Posts: 653

Taming the turntables a beat at a time


View Profile WWW
« Reply #6 on: March 22, 2007, 11:44:51 PM »

You don't even need that for Windows, io.h has _findfirst, _findnext, and _findclose functions which do exactly the same thing, with the added bonus you don't need the PSDK to use them.
Logged

Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.11 | SMF © 2006-2009, Simple Machines LLC Valid XHTML 1.0! Valid CSS!