Solved

C, how to get a list of files in a directory matching

Posted on 2012-03-21
3
240 Views
Last Modified: 2012-03-22
how to get a list of files from a directory matching a string (in C code)
0
Comment
Question by:rgbcof
  • 2
3 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 500 total points
ID: 37749463
For Unix/Linux, that's be 'opendir()' and 'readdir()', e.g.

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

int main(int argc, char **argv)
{
DIR *dirHandle;
struct dirent * dirEntry;

  dirHandle = opendir("."); /* open current directory */
  if (dirHandle) {
    while (0 != (dirEntry = readdir(dirHandle))) {
        puts(dirEntry->d_name);
    }
    closedir(dirHandle);
  }
}

Open in new window


You'll find the Windows equivalent using 'FindFirstFile()'/'FindNextFile()' here: http://msdn.microsoft.com/en-us/library/aa365200%28VS.85%29.aspx ("Listing the Files in a Directory")

What kind of "match" are you thinking of? One string can always only match one single file.
0
 

Author Closing Comment

by:rgbcof
ID: 37750329
Very good.  You assumed linux, and you are right.
0
 
LVL 86

Expert Comment

by:jkr
ID: 37750862
I'm a coder [beep], I don't care about the os in the end ;o)
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now