Solved

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

Posted on 2012-03-21
3
246 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
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…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.

688 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