Advertisement
Guest User

C++ Filesystem Operations Example

a guest
Aug 29th, 2014
264
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.99 KB | None | 0 0
  1. /* FILESYSTEM OPERATIONS EXAMPLE
  2.  *
  3.  * This program gets the list of files in the directory from which this program
  4.  * is ran, and outputs said list without sorting it.
  5.  *
  6.  * This program uses <dirent.h> to accomplish this. However, there's no
  7.  * standardized library to do this in C++.
  8.  *
  9.  * I apologize in advance for making this program run in its entirety within
  10.  * main().
  11.  */
  12.  
  13. #include <dirent.h>
  14. #include <iostream>
  15. using namespace std;
  16.  
  17. int main(){
  18.    
  19.     // initialize variables
  20.    
  21.     DIR *dp; // pointer used to store files list
  22.     struct dirent *dirp; // pointer to an item in files list
  23.     string db[64]; // database to which the files list will be written to
  24.     char index = 0;
  25.    
  26.     // open the directory and fill db[] with its contents
  27.     dp = opendir("./");
  28.     while((dirp = readdir(dp)) != NULL){
  29.         db[index] = string(dirp->d_name);
  30.         index ++;
  31.     }
  32.     closedir(dp);
  33.    
  34.     // output db[]
  35.     for(char i = 0; i < index; i ++){
  36.         cout << db[i] << endl;
  37.     }
  38.    
  39.     // the end
  40.     return 0;
  41.    
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement