-
-
Save iziang/8383747 to your computer and use it in GitHub Desktop.
指针一定要初始化,给其分配指向的内存地址!!!!!
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
free错误,透彻理解什么时候需要free!!!!