作为从我的一个类的任务的一部分,我必须在C中编写一个程序来复制ls -al命令的结果.我已经阅读了必要的材料,但我仍然没有得到正确的产出.这是我的代码到目前为止,它只应该打印出文件大小和文件名,但其打印的文件大小是不正确的.
码:
#include
#include
#include
#include
#include
#include
int main(int argc,char* argv[])
{
DIR *mydir;
struct dirent *myfile;
struct stat mystat;
mydir = opendir(argv[1]);
while((myfile = readdir(mydir)) != NULL)
{
stat(myfile->d_name,&mystat);
printf("%d",mystat.st_size);
printf(" %s\n",myfile->d_name);
}
closedir(mydir);
}
这些是执行代码后的结果:
[root@localhost ~]# ./a.out Downloads
4096 ..
4096 hw22.c
4096 ankur.txt
4096 .
4096 destination.txt
以下是正确的尺寸:
[root@localhost ~]# ls -al Downloads
total 20
drwxr-xr-x. 2 root root 4096 Nov 26 01:35 .
dr-xr-x---. 24 root root 4096 Nov 26 01:29 ..
-rw-r--r--. 1 root root 27 Nov 21 06:32 ankur.txt
-rw-r--r--. 1 root root 38 Nov 21 06:50 destination.txt
-rw-r--r--. 1 root root 1139 Nov 25 23:38 hw22.c
任何人都可以指出我的错误.
谢谢,
ANKUR