TechArena Community

TechArena Community (http://forums.techarena.in/)
-   Software Development (http://forums.techarena.in/software-development/)
-   -   Show directory contents in C (http://forums.techarena.in/software-development/1117021.htm)

Janet J 05-02-2009 11:33 PM

Show directory contents in C
 
I would like to create a program in c to view the contents of the directory, the compiler that I use is GCC and the system on which it is running linux.

check manuals and the internet I have a great help you can you guide me about it?

Programing forum could help to set the material also present in the internet to study this subject?

Thank you.

Astro121 05-02-2009 11:36 PM

Re: Show directory contents in C
 
I have listed the following code in one of my project but the following Program lists the files and subdirectories within a given directory full path

Code:

# include <stdio.h>
  # include <stdlib.h>
  # include <string.h>
  # include <dirent.h>

  char * path_cat (const char * str1, char * str2);

  int main () (
          struct dirent * dp;

          /  / Enter path to directory existing below
          const char * dir_path = "/ path / to / directory / to / list";
          DIR * dir = opendir (dir_path);
          while ((dp = readdir (dir))! = NULL) (
                  char * tmp;
                  tmp = path_cat (dir_path, dp -> d_name);
                  printf ( "% s \ n", tmp);
                  free (tmp);
                  tmp = NULL;
          )
          closedir (dir);
          return 0;
  )

  char * path_cat (const char * str1, char * str2) (
          size_t str1_len = strlen (str1);
          size_t str2_len = strlen (str2);
          char * result;
          result = malloc ((str1_len + str2_len + 1) * sizeof * result);
          strcpy (result, str1);
          int i, j;
          for (i = str1_len, j = 0; ((i <(str1_len + str2_len)) & & (j <str2_len)) i + +, j + +) (
                  result [i] = str2 [j];
          )
          result [str1_len + str2_len] = '\ 0';
          return result;
  )

Hope this helps...

GNom 05-02-2009 11:43 PM

Re: Show directory contents in C
 
User manual for displaying directory contents

NAME

readdir - read a directory

SYNOPSIS
Code:

# include <sys / types.h>

  # include <dirent.h>

  struct dirent * readdir (DIR * dir);

DESCRIPTION

The readdir () function returns a pointer to a dirent structure representing the next directory entry in the directory stream pointed to by dir. It returns NULL on reaching the end-of-file or if an error occurred.

According to POSIX, the dirent structure contains a field char d_name [] of unspecified size, with at most NAME_MAX characters preceding the terminating null character. Use of other fields will harm the portability of your programs. POSIX 1003.1-2001 also documents the field ino_t d_ino as an XSI extension.

The data returned by readdir () may be overwritten by subsequent calls to readdir () for the same directory stream.

RETURN VALUE

The readdir () function returns a pointer to a dirent structure, or NULL if an error occurs or end-of-file is reached.


All times are GMT +5.5. The time now is 09:08 PM.