Results 1 to 3 of 3

Thread: Show directory contents in C

  1. #1
    Join Date
    Jan 2009
    Posts
    150

    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.

  2. #2
    Join Date
    Jan 2009
    Posts
    99

    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...

  3. #3
    Join Date
    Dec 2008
    Posts
    70

    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.

Similar Threads

  1. Nokia N9 mobile phone cannot show all media contents
    By Aminah in forum Portable Devices
    Replies: 4
    Last Post: 19-01-2012, 10:41 PM
  2. Replies: 6
    Last Post: 27-07-2011, 07:12 PM
  3. Delete the contents of a directory using Batch
    By GUSSIE in forum Operating Systems
    Replies: 3
    Last Post: 11-09-2009, 12:43 PM
  4. Virtual Directory does not allow contents to be listed
    By Sachit in forum Networking & Security
    Replies: 3
    Last Post: 11-07-2009, 09:54 AM
  5. What are the contents of Ms DOS.sys
    By Jecco in forum Windows Software
    Replies: 3
    Last Post: 11-06-2009, 12:20 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,718,832,412.79127 seconds with 17 queries