-
-
Save imduffy15/5521102 to your computer and use it in GitHub Desktop.
This file contains hidden or 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<stdlib.h> | |
| #include <sys/types.h> | |
| #include <dirent.h> | |
| int main(void) { | |
| char *curr_dir = NULL; | |
| DIR *dp = NULL; | |
| struct dirent *dptr = NULL; | |
| unsigned int count = 0; | |
| // Get the value of environment variable PWD | |
| curr_dir = getenv("PWD"); | |
| if(NULL == curr_dir) { | |
| printf("\n ERROR : Could not get the working directory\n"); | |
| return -1; | |
| } | |
| // Open the current directory | |
| dp = opendir((const char*)curr_dir); | |
| if(NULL == dp) { | |
| printf("\n ERROR : Could not open the working directory\n"); | |
| return -1; | |
| } | |
| printf("\n"); | |
| // Go through and display all the names (files or folders) | |
| // Contained in the directory. | |
| for(count = 0; NULL != (dptr = readdir(dp)); count++) { | |
| printf("%s ",dptr->d_name); | |
| } | |
| printf("\n %u", count); | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment