I'm not able to identify the error thrown by stat. The below program reads all files in a directory and prints the file name:
DIR *dp;
struct dirent *dirp;
struct stat sb;
if((dp = opendir(argv[1]))==NULL)
{
perror("can't open dir");
}
while((dirp = readdir(dp))!=NULL)
{
if (stat(dirp->d_name, &sb) == -1) {
perror("stat");
}
printf("File name: %s
",dirp->d_name);
}
Sample output:
/home/eipe
stat error: No such file or directory
File name: copyofsample
File name: a.out
File name: .
stat error: No such file or directory
File name: udpclient.c
File name: ..
stat error: No such file or directory
File name: client.c
stat error: No such file or directory
File name: ftpclient.c
Here are the contents:
ls -l /home/eipe/c
-rwxr-xr-x 1 eipe egroup 7751 2011-02-24 15:18 a.out
-rw-r--r-- 1 eipe egroup 798 2011-02-24 13:50 client.c
-rw-r--r-- 1 eipe egroup 15 2011-02-24 15:34 copyofsample
-rw-r--r-- 1 eipe egroup 1795 2011-02-24 15:33 ftpclient.c
-rw-r--r-- 1 eipe egroup 929 2011-02-24 13:34 udpclient.c
Question&Answers:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…