close
我需要判定資料夾下面是否有檔案存在的程式:

來看看神人寫的程式

也可以參考:http://linux.die.net/man/3/readdir

++++++++++++++++++++++++++++++++++++++++++<( ̄︶ ̄)>
#include
#include
#include
#include
#include
#include

int read(char *pth)
{
    char path[1000];
    strcpy(path,pth);
    DIR *dp;
    struct dirent *files;
    /*structure for storing inode numbers and files in dir
    struct dirent
    {
        ino_t d_ino;
        char d_name[NAME_MAX+1]
    }
    */
    if((dp=opendir(path))==NULL)
        perror("dir\n");
    char newp[1000];
    struct stat buf;
    while((files=readdir(dp))!=NULL)
    {
              if(!strcmp(files->d_name,".") || !strcmp(files->d_name,".."))
                continue;

        strcpy(newp,path);
        strcat(newp,"/");
        strcat(newp,files->d_name);
            printf("%s\n",newp);

            //stat function return a structure of information about the file
        if(stat(newp,&buf)==-1)
        perror("stat");
        if(S_ISDIR(buf.st_mode))// if directory, then add a "/" to current path
        {

            strcat(path,"/");
            strcat(path,files->d_name);
            read(path);
            strcpy(path,pth);
        }
    }
}
int main(int argc,char *argv[])
{

    read(argv[1]);
}


++++++++++++++++++++++++++++++++++++++++
執行結果:
[root@mos01_linux ScanfFile]# ./main ./
.//main
.//a.ready
.//main.cpp
.//b.ready
.//a.out
+++++++++++++++++++++++++++++++++++++++++
arrow
arrow
    全站熱搜

    rangerll 發表在 痞客邦 留言(0) 人氣()