creating directories using c functions???

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • samreen
    New Member
    • Oct 2006
    • 2

    creating directories using c functions???

    i want to create and access directories using C functions...... .......could u please help me out regarding this....
  • Guy Lusilao
    New Member
    • Oct 2006
    • 3

    #2
    You must have this header in your file
    #include <stdlib.h>
    and use this command in your c++ /C code to create a directory
    system("mkdir MYDIRECTORY");
    where MYDIRECTORY is the name of your directory
    and to access this directory, just do
    system("chdir MYDIRECTORY");

    Comment

    • samreen
      New Member
      • Oct 2006
      • 2

      #3
      Originally posted by Guy Lusilao
      You must have this header in your file
      #include <stdlib.h>
      and use this command in your c++ /C code to create a directory
      system("mkdir MYDIRECTORY");
      where MYDIRECTORY is the name of your directory
      and to access this directory, just do
      system("chdir MYDIRECTORY");
      hi,
      thankx i tried it out, but i'm not able to figure out how can i create file in this directory or how can i list or determine how many files exist in a particular directory using C....please help out

      Comment

      • Jai Vrat Singh
        New Member
        • Oct 2006
        • 18

        #4
        a small sample.. use it to count or to print :)
        Code:
        int printFiles(char * dirname){
        
             DIR* dirp;
             dirp = opendir(dirname);
             struct dirent * dp=0;
             int i=0;
             while (dirp) {
             i++;
             std::cout<<" In while loop "<<i<<std::endl;
                 errno = 0;
                 if ((dp = readdir(dirp)) != NULL) {
                   std::string curr(dp->d_name);
                   std::cout<<" dp->d_name = "<<curr<<std::endl;
                 } else {
                   break;
                 }
             }
             closedir(dirp);
        
             return 0;
        }
        
        int countFiles(char * dirname){
        
             DIR* dirp;
             dirp = opendir(dirname);
             struct dirent * dp=0;
             int i=0;
             while (dirp) {
              i++;
              if ((dp = readdir(dirp)) == NULL) break;
             }
             closedir(dirp);
        
             return i;
        }
        I have written this hastily ..please test for bugs...

        Comment

        • Jai Vrat Singh
          New Member
          • Oct 2006
          • 18

          #5
          Full code:
          Code:
               1  #include<iostream>
               2  #include<cstdio>
               3  #include <sys/types.h>
               4  #include <dirent.h>
               5  #include <cstdlib>
               6  #include <cerrno>
               7
               8  int printFiles(char * dirname);
               9  int countFiles(char * dirname);
              10  int main( int argc, char *argv[]){
              11
              12  printf("argv[1] is %s\n",argv[1]);
              13  printFiles( argv[1]);
              14  printf("Number of files in %s is %d\n",argv[1],countFiles(argv[1]));
              15  return(0);
              16  } 
              17
              18  int printFiles(char * dirname){
              19
              20       DIR* dirp;
              21       dirp = opendir(dirname);
              22       struct dirent * dp=0;
              23       int i=0;
              24       while (dirp) {
              25       i++;
              26       std::cout<<" In while loop "<<i<<std::endl;
              27           errno = 0;
              28           if ((dp = readdir(dirp)) != NULL) {
              29             std::string curr(dp->d_name);
              30             std::cout<<" dp->d_name = "<<curr<<std::endl;
              31           } else {
              32             break;
              33           }
              34       }
              35       closedir(dirp);
              36           
              37       return 0;
              38  }
              39
              40  int countFiles(char * dirname){
              41
              42       DIR* dirp;
              43       dirp = opendir(dirname);
              44       struct dirent * dp=0;
              45       int i=0;
              46       while (dirp) {
              47        i++;
              48        if ((dp = readdir(dirp)) == NULL) break;
              49       }
              50       closedir(dirp);
              51           
              52       return i;
              53  }

          Comment

          Working...