Skip to content

Instantly share code, notes, and snippets.

@iziang
Last active January 3, 2016 00:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iziang/8383747 to your computer and use it in GitHub Desktop.
Save iziang/8383747 to your computer and use it in GitHub Desktop.
指针一定要初始化,给其分配指向的内存地址!!!!!
#include <stdio.h>
#include <dirent.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int main(int argc, char *argv[])
{
if (argc != 2) {
printf("Please input directory or file name\n");
return 1;
}
struct stat file_attr;
struct dirent *file;
if(lstat(argv[1], &file_attr) < 0) {
printf("lstat error\n");
return 1;
}
if (S_ISDIR(file_attr.st_mode)) {
struct dirent *file;
DIR *dirp = opendir(argv[1]);
while ((file = readdir(dirp)) != NULL) {
printf("%s\n", file->d_name);
}
closedir(dirp);
} else {
printf("%s\n", argv[1]);
}
return 0;
}
@iziang
Copy link
Author

iziang commented Jan 12, 2014

free错误,透彻理解什么时候需要free!!!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment