Solved

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

Posted on 2012-03-21
3
242 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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Dll Dynamic way c/c++ 9 145
Which checksum is this? 7 144
How to get time_t value that is a week old 6 168
xamarin c# deserialize Json containing nested object 2 93
An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
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…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.

810 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