Jump to content

Directory listing in C

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
3 replies to this topic

#1
novice

novice

    Newbie

  • Members
  • PipPip
  • 13 posts
Guys, I am trying to make a program for myself to run on Windows and Linux platforms.

The program I am attempting to make is a Directory Listing program. The program should give me the contents of the directory {just the name of the files in the directory}

Eg : If the user enters a directory say "C:\test" {in Windows}, and if the folder "test" has files called "abc.txt", "def.html" etc and a folder called "subtest", then when I run my program, the output should be

The directory : C:\test has the following:


subtest

abc.txt

def.html

Can you please guide me as to how I should go about making such a program.

#2
Guest_Jordan_*

Guest_Jordan_*
  • Guests
I found this code:

     #include <stdio.h>
     #include <sys/types.h>
     #include <dirent.h>
     
     int
     main (void)
     {
       DIR *dp;
       struct dirent *ep;
     
       dp = opendir ("./");
       if (dp != NULL)
         {
           while (ep = readdir (dp))
             puts (ep->d_name);
           (void) closedir (dp);
         }
       else
         perror ("Couldn't open the directory");
     
       return 0;
     }

From here: Simple Directory Lister - The GNU C Library

#3
novice

novice

    Newbie

  • Members
  • PipPip
  • 13 posts
^^ thanks mate... but i got a few questions :

1. is it cross platform ?? [i've checked it on my windows machine and it works but what about linux ?? ]

2. if I want to implement some kind of search function in this, how should I go about it ?

#4
Aereshaa

Aereshaa

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 790 posts
Filesystems are OS-dependent.