LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   For those looking for a quick solution for opendir readdir (https://www.linuxquestions.org/questions/programming-9/for-those-looking-for-a-quick-solution-for-opendir-readdir-4175434688/)

gothrog 10-29-2012 11:03 PM

For those looking for a quick solution for opendir readdir
 
Reading files in a directory using @c++:

It seems that "ls" causes some problems so if you read around enough @C++ developers tell you to use @opendir and @readdir.

I'm posting this here mainly because I never found a code solution that was in one piece, so I'm adding this code for some quick public searching.

Code:

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

// This reads only the files and not folders
unsigned char isFile =0x8;
//unsigned char isFolder =0x4; // Reads only a folder

char buff[256] = {"."};

DIR *Dir = new DIR();
struct dirent *DirEntry;

if((Dir=opendir(buff))==NULL)
{
  printf("Error");
  exit(1);
}
else
{
  cout << "OpenDir is open" << endl;
  while(DirEntry=readdir(Dir))
  {
      if ( DirEntry->d_type == isFile)
      {
      cout <<"Found a File : " << DirEntry->d_name << endl;
      }
  }
}


millgates 10-30-2012 09:21 AM

Hi, nice program! Just a few notes:
1) Your program is missing main()
2)
Code:

DIR *Dir = new DIR();
if((Dir=opendir(buff))==NULL) { ... }

will cause a memory leak

3) You shouldn't mix stdio with iostream

bigearsbilly 10-30-2012 06:36 PM

Or to really cheat:
(note no error checks)
edit: except it doesn't really work properly, oops
need to use glob as well, I'll fix it tomorrow.


Code:

#include <stdio.h>
#include <wordexp.h>
#include <stdlib.h>

int main ()
{
        char ** p;
        wordexp_t pp;

        wordexp("~/* ./*", &pp, 0);

        printf("found : %u\n", pp.we_wordc);
        for ( p = pp.we_wordv; *p; ++p) {
                printf("file: %s\n", *p);
        }
        wordfree(&pp);
        return 0;
}

Code:

found : 29
file: /home/mark/1
file: /home/mark/2
file: /home/mark/Audio Disc.toc
file: /home/mark/Audio Disc.toc.bin
file: /home/mark/Desktop
file: /home/mark/Documents
file: /home/mark/Downloads
file: /home/mark/Music
file: /home/mark/Pictures
file: /home/mark/Public
file: /home/mark/Templates
file: /home/mark/Videos
file: /home/mark/korn
file: /home/mark/korn.c
file: /home/mark/sharon
file: /home/mark/stuff.txt
file: ./Makefile
file: ./eg
file: ./eg.c
file: ./needle
file: ./needle.cpp
file: ./readline
file: ./readline.cpp
file: ./regex
file: ./regex.cpp
file: ./wordexp
file: ./wordexp.c


gothrog 11-01-2012 02:21 PM

@millgates - Thanks I didn't know about the stdio vs iostream issues.

@bigearsbilly - Thanks you have a better solution and can run on Centos 5 and Cygwin.
This line of code can compile and run on cygwin, however on Centos 5 it can't compile.
Code:

DIR *Dir = new DIR();
@bigearsbilly - Thanks again.

bigearsbilly 11-01-2012 03:20 PM

Don't thank me too soon, it doesn't quite work.
If one does say '*.c' and there's no match still it will
print '*.c'

so it needs a further glob to work properly.


All times are GMT -5. The time now is 09:32 AM.