下面代码实现类似Linux终端中ls功能
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <dirent.h>
int main(int argc, char *argv[])
{
DIR *dp;
struct dirent *sdp;
dp = opendir(argv[1]);
if (dp==NULL)
{
perror("opendir error");
exit(1);
}
printf("----open ok----\n");
while(sdp = readdir(dp))
{
printf("%s\t", sdp->d_name);
}
printf("\n");
closedir(dp);
return 0;
}