Solved

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

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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Want to delete all my personal data 13 146
Parsing XML instructions in Java/any other language.... 3 116
C++ finding a sting in a char* string from a text file 3 120
Line meaning 9 87
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-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.

840 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