I am having a bit of trouble creating a C program that reads the current directory, prints the file path, and contents.
For each file found in the directory the contents should be printed based on whether they are a directory, a file, or executable.
I have the main components working just unsure how to sort the files output after using the opendir() / closedir() command
eg. end output:
/home/documents/folder1
File: help.txt
File: me.txt
Executable: plz
File: thankyou.c
Current code:
struct dirent *de; // Pointer for directory entry
// opendir() returns a pointer of DIR type.
DIR *dir = opendir(".");//opens current direcotry
if (dir == NULL) // opendir returns NULL if couldn't open directory
{
printf("ERROR: Could not open current directory" );
return 1;
}
// for readdir()
while ((de = readdir(dir)) != NULL){
//if (Executable){}
//else if (File){}
//else if (Directory){}
printf("%s
", de->d_name);
See Question&Answers more detail:os