Monday, May 18, 2009

Fibroid On Arch Of Foot

List directory contents C-language Linux Guatemalan

Hello
such at all? in this post take the opportunity to explain how we can list the contents of a directory in Linux (in my case I did it from Ubuntu 8.10 using C language

the example shown here I did based on the example that is in the following link:

http://snippets.dzone.

com/posts/show/5734 However, the method used path_cat that there did not seem of any use and remove and printed directly to console. You try to comment all the code to understand what each line. For documentation the code uses the following link:

http://opengroup.org/onlinepubs/007908799/xsh/dirent.h.html

This link explains the dirent.h library that is used to list directories and the methods that have so as all its features.

in the code that changes do not go into detail because their comments are more than clear.

To compile this code use the following command:

listar.c gcc-o list listar.c

Where is the name you put your little program created in C.

The code is as follows:



/ * Erick Orrego Lorini Efrain
http://blogdelbaterista.blogspot.com
Guatemala, March 19, 2009 Based on http://snippets.dzone.com/posts
/ show/5734 * /


# include \u0026lt;stdio.h >
# include \u0026lt;stdlib.h >
# include \u0026lt;string.h >
/ / library that lets you list directories
# include
\u0026lt; dirent.h >

int main () {struct dirent * d
;

/ / declare the path of the direction we wish list
const char * address = "/ home "
/ * DIR represents the stream for a directory. With opendir
open the directory that was specified in direction * /
DIR * directory = opendir (address);
/ * until it has traveled the directory * /
/ * readdir returns a pointer to a structure that represents the
a directory entry in the current position in the directory stream * /
/ / display the contents of whom we will list
printf ("Contents of% s \\ n \\ n", address);
while ((d = readdir (directory))! = NULL) {
/ / print directory base / subdirectory
printf ("% s /% s \\ n", address, d-> d_name);}

/ / close the directory stream
closedir (directory);
return 0;}



and with this you can perform the implementation for the user to enter the path they want to list or anything else you need done with directory listing, I hope it is useful to you, We appreciate the comments, greetings.

If the user enters the desired directory path list and this directory does not exist, opendir () will return a NULL.

0 comments:

Post a Comment