LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 10-29-2012, 11:03 PM   #1
gothrog
Member
 
Registered: Jun 2004
Distribution: Yellow Dog, Fedora, RedHat, Centos, Ubuntu, Suse Linux
Posts: 106

Rep: Reputation: 15
Smile 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;
      }
   }
}

Last edited by gothrog; 10-29-2012 at 11:05 PM.
 
Old 10-30-2012, 09:21 AM   #2
millgates
Member
 
Registered: Feb 2009
Location: 192.168.x.x
Distribution: Slackware
Posts: 852

Rep: Reputation: 389Reputation: 389Reputation: 389Reputation: 389
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
 
1 members found this post helpful.
Old 10-30-2012, 06:36 PM   #3
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
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

Last edited by bigearsbilly; 10-30-2012 at 06:45 PM. Reason: rubbish
 
1 members found this post helpful.
Old 11-01-2012, 02:21 PM   #4
gothrog
Member
 
Registered: Jun 2004
Distribution: Yellow Dog, Fedora, RedHat, Centos, Ubuntu, Suse Linux
Posts: 106

Original Poster
Rep: Reputation: 15
@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.
 
Old 11-01-2012, 03:20 PM   #5
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
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.
 
  


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



Similar Threads
Thread Thread Starter Forum Replies Last Post
Solution to Quick Fix MadnessASAP Linux - Software 1 06-03-2005 08:08 PM
In a smtp bind and need a quick solution bhughesiii Linux - Networking 2 05-10-2005 01:36 PM
Quick backup solution... new@linux Linux - Newbie 1 12-08-2004 04:54 PM
Opening a directory WITHOUT using 'opendir' and 'readdir' skywalker27182 Programming 2 11-16-2003 01:32 PM
quick fstab solution louie055 Linux - Newbie 1 09-01-2003 02:00 PM

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

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