Need help with scandir / stat


 
Thread Tools Search this Thread
Top Forums Programming Need help with scandir / stat
# 1  
Old 02-05-2005
Need help with scandir / stat

I'm writing a file manager program using FC3 and C, and I'm having a problem displaying the stat info of subdirectories.

Code:
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>

int main()
{
   char *dirname = "mydirectory";
   struct dirent **namelist;
   struct stat buf;
   int n = scandir(dirname, &namelist, 0, alphasort);
   int i;


   for ( i = 0; i < n; i++ )
   { 
      char *file_name = namelist[i]->d_name;
      if ( stat( file_name, &buf ) )
         perror( "stat" );
   }
   
   return 0;
}

I keep getting a "No such file or directory" error, which doesn't make sense because namelist contains the names of the files in mydirectory. The code only works if dirname is "." or if file_name in the stat function is replaced with the actual names of the files in a subdirectory. Can anyone help me with this?
# 2  
Old 02-05-2005
You need to chdir() to the directory.
# 3  
Old 02-07-2005
Or supply stat() with a fully qualifed path - /home//bertsura/mydirs/dir1/subdir1
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Stat value changes

Die to what all operations, the "Modify" and "Change" values of stat output changes for a file. I found, during editing a file, Change and Modify alters. When chmod'ing Change alters, while Modify doesnot alters. Is there more situations where these changes? (1 Reply)
Discussion started by: anil510
1 Replies

2. UNIX for Dummies Questions & Answers

Stat command

i know this command does not exist in solaris. however, i read somewhere on this forum that basically everything the stat command provides in other oses can be obtained in solaris using the ls command. i've searched the forum for a while now and i cant find the thread. does anyone know about... (1 Reply)
Discussion started by: SkySmart
1 Replies

3. Programming

scandir() and threads

I couldn't find anywhere informations about it. Is scandir() thread-safe? (4 Replies)
Discussion started by: dawwin
4 Replies

4. UNIX for Advanced & Expert Users

scandir help

Hi everyone; I am trying to compile and execute a test program I wrote that calls scandir and it seems to not like my prototype. The Documentation on SUN for this function is clear, but I am missed something..... and I can't figure it out; I am using cc -gv -osaveas saveas.c on Sun... (1 Reply)
Discussion started by: dhelie
1 Replies

5. Solaris

stat: Available on Solaris?

JoeyG's note in the following thread got me thinking about using stat more often in file operations. I've only ever used it within perl - didn't even realize there was a commandline version of it.... (3 Replies)
Discussion started by: Smiling Dragon
3 Replies

6. Programming

Q with stat()

From reading various articles on the net, I know stat() is used on files to get things like permissions, sizes etc... As a folder is a special type of file in Unix, I assumed that stat() could work on it as well as any general file. However, from running my program, perror() reported that the... (3 Replies)
Discussion started by: JamesGoh
3 Replies

7. UNIX for Advanced & Expert Users

stat

the output of stat command is Size: 238 Blocks: 8 IO Block: 4096 regular file Device: 80ah/2058d Inode: 736783 Links: 1 Access: (0664/-rw-rw-r--) Uid: ( 500/ gajju) Gid: ( 500/ gajju) Access: 2008-09-08 20:00:15.000000000 +0530 Modify: 2008-09-08... (6 Replies)
Discussion started by: gajju
6 Replies

8. Programming

scandir() + windows equivalent

Currently, Im trying to redesign some Unix-based C code to work on the Windows operating system and one problem I ran into when compiling was that the compiler failed to recognise scandir() (from my original Linux code) Im aware that scandir() is a Unix-based function, so is there an equivalent... (1 Reply)
Discussion started by: JamesGoh
1 Replies

9. Programming

scandir() for Solaris

I'd like to use scandir() on solaris, but it doesn't find sys/dir.h What do you think I should do? thanks (2 Replies)
Discussion started by: nadiamihu
2 Replies

10. UNIX for Dummies Questions & Answers

Cannot stat?

Hi! I ran into a problem with a job I'm running. All it is doing is a "touch" on a filename. However, when I ran the job, it error'd out and got the message 'cannot stat'. When I restarted the job (making no changes) it worked just fine. Anyone know what this means? (1 Reply)
Discussion started by: lgardner
1 Replies
Login or Register to Ask a Question