help with printing file names in current directory for C


 
Thread Tools Search this Thread
Top Forums Programming help with printing file names in current directory for C
# 1  
Old 02-11-2011
help with printing file names in current directory for C

How do I print all the file names in current directory in C?
# 2  
Old 02-11-2011
Well, dirent() is a touchy little bit of code, so read the man page carefully else you SEGV. You can also popen( "ls -1", "r" ) but that is cheating.
# 3  
Old 02-11-2011
this code seems to do the trick but can you explain how it works (like what each line means)?
Code:
        DIR *d;
        struct dirent *dir;
        d = opendir(".");
        if (d) {
            while ((dir = readdir(d)) != NULL) {
                printf("%s\n", dir->d_name);
            }
            closedir(d);
        }

# 4  
Old 02-11-2011
Pull down the stack pointer to create space for a DIR pointer called d and a struct dirent pointer called dir. Call opendir to get a starting dirent for ".". If it is not null (see man opendir for returns and errno's), then call readdir() on d to put a struct dirent * into dir, which is a static storage inside readdir and probably not thread-safe. If the pointer returned is not null, dereference it to a char* of its d_name, print that and a new line to the stdout buffered FILE* and loop back to call readdir(), else call closedir() on d to close the FD open to read the directory.

Directories contain just entry names and inode numbers. Directories grow but never shrink, as deletions are just nulled out. A flat file can have its inode number in man directories, and there is a link count on the inode so it can tell if it is completely deleted. You can see this on ls -l.
# 5  
Old 02-11-2011
Can anyone explain stat, i want to use it to print the filenames of all files in current directory.

Last edited by omega666; 02-11-2011 at 05:25 PM..
# 6  
Old 02-11-2011
stat()
# 7  
Old 02-11-2011
not working...
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Printing filenames in my current directory

Can someone give me a tip on writing a script that, for each file in the working directory, prints the filename, the # of lines, and the # of words to stdout? (2 Replies)
Discussion started by: flash123
2 Replies

2. UNIX for Dummies Questions & Answers

List Directory names which have the file

Hi All, Can any one help me to list out the directory names which contain the specified file. See for example File name : file.201307014.LKT Have the directory structure as below. /app/work/data/INDIA/file.201307014.LKT /app/work/data/AMERICA/file.201307014.KTP... (5 Replies)
Discussion started by: Balasankar
5 Replies

3. UNIX for Dummies Questions & Answers

Locate the column names with their values in the file and the printing the same in the other file

I have text file in Linux with two rows : first row conmtain the column nam and the second row contain its value .I nned to fetch few columns first and then redirect the data of those colum in the another file. Any ideas?? (1 Reply)
Discussion started by: Anamica
1 Replies

4. Shell Programming and Scripting

Grepping file names, comparing them to a directory of files, and moving them into a new directory

got it figured out :) (1 Reply)
Discussion started by: sHockz
1 Replies

5. UNIX for Dummies Questions & Answers

find the file names having specified pattern at specified position in the current directory

I would need a command for finding first 15000 of the file names whose 25th postion is 5 in the current directory alone. I do have this painful command find . -name '5*' | head -15000 | cut -c3- please refine this. Of course the above command also searches in the sub directories... (3 Replies)
Discussion started by: vk39221
3 Replies

6. Shell Programming and Scripting

Searching for file names in a directory while ignoring certain file names

Sun Solaris Unix Question Haven't been able to find any solution for this situation. Let's just say the file names listed below exist in a directory. I want the find command to find all files in this directory but at the same time I want to eliminate certain file names or files with certain... (2 Replies)
Discussion started by: 2reperry
2 Replies

7. Shell Programming and Scripting

Finding files in current directory when 100,000's files in current directory

Hi All I was wondering what is the most efficient way to find files in the current directory(that may contain 100,000's files), that meets a certain specified file type and of a certain age. I have experimented with the find command in unix but it also searches all sub directories. I have... (2 Replies)
Discussion started by: kewong007
2 Replies

8. Shell Programming and Scripting

Unable to see all file in a current directory

Hi, I am unable to see all files in a current directory when use "ls -lrt" command it is giving error message as below ( I think this current directory is having about 500 files) <CONTROL /home/ckanth/sri>ls -lrt UX:ls: ERROR: Out of memory: Insufficient or invalid memory But when i... (3 Replies)
Discussion started by: srikanthus2002
3 Replies

9. Shell Programming and Scripting

directory names in a flat file

Hi, Consider a flat file abc.conf contains some rows. Each row contains the directory name with full path. now I want to find a particular file in every directory which are mentioned in the abc.conf file. How it can be done through unix shell script. (2 Replies)
Discussion started by: surjyap
2 Replies

10. UNIX for Advanced & Expert Users

File and Directory Names become hidden

Something very weird has been happening when I'm creating files and directories. When I create a directory, at times depending on the directory name and depth, it becomes hidden and can only be seen typing "ls -a". When I say the name of the directory matters, "my_c++" will be hidden but using... (10 Replies)
Discussion started by: dbinsol1
10 Replies
Login or Register to Ask a Question