Linux under Scan directory put all the plain text in the list

Source: Internet
Author: User
Tags sprintf

#include <stdio.h> #include <stdlib.h> #include <string.h> #include <sys/stat.h> #include < dirent.h> #include <unistd.h>const static int strarraylen=256;typedef char Stringbuffer[strarraylen];typedef    struct stringlist_t stringlist;struct stringlist_t{stringbuffer sb;    Stringlist *next;        stringlist_t () {next = 0; memset (SB, 0, sizeof (SB));    sb[0]= ' + ';        } ~stringlist_t () {stringlist *itor = this->next;            while (itor! = NULL) {This->next = itor->next;            Delete Itor;        Itor = this->next;    }}};void outputlist (Stringlist *list) {stringlist *itor = list;        while (itor! = 0) {printf ("%s\n", ITOR-&GT;SB);    Itor = itor->next; }}int scanfilestolist (char* folderpath, stringlist *filelist) {//get rid of '/' within folderpath int slen = St    Rlen (FolderPath);    if (folderpath[slen-1] = = '/') {folderpath[slen-1] = ' + '; } StRingbuffer pwd = "";    GETCWD (pwd, sizeof (PWD));        if (strncmp (FolderPath, ".", 2) = = 0) {char pwdbak[200] = "";        strcpy (Pwdbak, PWD);        ChDir (FolderPath);        memset (pwd, 0, sizeof (PWD));        GETCWD (pwd, sizeof (PWD));        strcpy (FolderPath, PWD);    strcpy (pwd, Pwdbak);    } else {chdir (folderpath);    } DIR *dir = Opendir (FolderPath);    if (dir = = NULL) {printf ("Failed to open dir <%s> \ n", FolderPath);    } struct Dirent *dt = NULL;    struct stat statbuf;        while (dt = Readdir (dir)) {if ('. ' = = Dt->d_name[0]) {continue;        } lstat (Dt->d_name, &statbuf);            if (S_isdir (Statbuf.st_mode)) {StringBuffer SB = "";            sprintf (SB, "%s/%s", FolderPath, Dt->d_name);            ChDir (SB);            Scanfilestolist (SB, fileList);        ChDir (".."); } else if (S_isreg (Statbuf.st_mode)) {//addfiles to list stringlist *tmp=newStringlist;            sprintf (TMP-&GT;SB, "%s/%s", FolderPath, Dt->d_name);            tmp->next=filelist->next;        filelist->next=tmp;    }} chdir (PWD); return 0;}

Linux scans the directory to list all plain text

Related Article

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.