Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read

Reply
 
Thread Tools Search this Thread
  #1  
Old 05-02-2009
Member
 
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.
Reply With Quote
  #2  
Old 05-02-2009
Member
 
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...
Reply With Quote
  #3  
Old 05-02-2009
Member
 
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.
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "Show directory contents in C"
Thread Thread Starter Forum Replies Last Post
Nokia N9 mobile phone cannot show all media contents Aminah Portable Devices 4 19-01-2012 10:41 PM
Transcend External Hard Disk Drive detects but do not show contents on windows 7 Franscisco Portable Devices 6 27-07-2011 07:12 PM
Delete the contents of a directory using Batch GUSSIE Operating Systems 3 11-09-2009 12:43 PM
Virtual Directory does not allow contents to be listed Sachit Networking & Security 3 11-07-2009 09:54 AM
What are the contents of Ms DOS.sys Jecco Windows Software 3 11-06-2009 12:20 PM


All times are GMT +5.5. The time now is 12:39 AM.