Directory operations (C language)

Source: Internet
Author: User

Directory operations (C language) header file:
    • #include <unistd.h>
    • #include <sys/stat.h>
    • #include <dirent.h>
Read Directory

Required header files: #include <dirent.h>

  • Open the directory and return an empty pointer at the time of the failure.

    DIR * OPENDIR (const char *path)

  • Reading a directory Readdir,readdir equivalent to a file operation function Read,dir struct equivalent to a file structure inside a document operation.

    struct Dirent *readdir (DIR *pdir)

    Dirent Structural Body:

    struct dirent  {  long d_ino; /* inode number 索引节点号 */ off_t d_off; /* 在目录文件中的偏移 */ unsigned short d_reclen; /* 文件名长 */ unsigned char d_type; /* 文件类型 */ char d_name [NAME_MAX+1]; /*文件名,最长256字符 */ }
  • Close Directory

    int Closedir (DIR *pdir)

To create a directory:

int mkdir (const char *path, mode_t mode)

To delete a directory:

int rmdir (char *path)

Get the current directory absolute path:
    • char * GETCWD (char *path, size_t size)

      GETCWD Success returns the current working directory, failure returns FALSE

      Parameters: The absolute path of the current working directory is copied to the memory space referred to in the parameter path, and the parameter size is the space size of path.

Setting path to the current working directory requires X permissions on the directory:

ChDir (char *path)

Set FD corresponding directory as current working directory:

Fchdir (int fd)

Example:

#Include<stdio.h>#Include<string.h>#Include<sys/stat.h>#Include<sys/types.h>#Include<unistd.h>#Include<errno.h>#Include<dirent.h>IntGetfiletype(Char *filepath,Char *type) {if (filepath==NULL) {perror ("The FilePath is not NULL");Return-1;}structStatStbuf;if (stat (FILEPATH,&AMP;STBUF)) {fprintf (StdErr"Fail to stat file[%s]:%s\n", Filepath,strerror (errno));Return-1;}Switch (STBUF.ST_MODE&AMP;S_IFMT) {Case S_ifdir:*type=' d ';BreakCase S_ifreg:*type=‘-‘;BreakDefault:*type=' O ';break;}Return0;}IntTree(Char *filepath,int deep) {DIR *pdir;structDirent *Pdent;Char Type;pdir=opendir (FilePath);int i;Char szsubdir[256];if (pdir==NULL) {fprintf (StdErr"Faie to open:[%s]", FilePath);Return0;}sprintf (Szsubdir,"./%s", FilePath); ChDir (Szsubdir);While1) {Pdent=readdir (pdir);if (pdent==NULL) {break;}Ifstrcmp (Pdent->d_name,".") ==0| |strcmp (Pdent->d_name,"..") ==0) {Continue;}for (i=0;i<deep;i++) {printf ("----");} printf ("%s\n", pdent->d_name); Getfiletype (Pdent->d_name,&type); if (type==' d ') {Tree (pdent->d_name,deep+1);}} ChDir (".. /"); Closedir (Pdir); return 0;} int main() {char szbuf[256];getcwd (szbuf,256); Tree (Szbuf,1);  return 0;}                   
Some other functions about directory operations:
    • void Seekdir (DIR *pdir, long int locate)

      Similar to Fseek ()

    • void Rewinddir (DIR *pdir)

      Similar to Rewind ()

    • Long int telldir (DIR *pdir)

      Similar to Ftell ()

Directory operations (C language)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.