文件目录操作opendir、readdir

#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <unistd.h>
#include <stdlib.h>
#include <netinet/in.h>
#include <dirent.h>
#include <arpa/inet.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <signal.h>
#include <pthread.h>
#include <semaphore.h>
#include <errno.h>
#include <stdio.h>
#include <fcntl.h>
#include <string.h>

int FileNameScan(char *pcPath)
{

    DIR             *pFile      = NULL;        //打开目录返回的指针
    struct dirent   *psDirInfo  = NULL;        //读取目录返回的结构图指针
    unsigned char    iRetValue;
    pFile = opendir(pcPath);             //打开目录
    if(NULL == pFile)
    {
        printf("DevXmlAnalysis.c: %s is not exist!\n", pcPath);
        return -1;          //如果目录为空,返回-1
    }
    else 
    {
     /*
        1、也就是说readdir每调用一次,pcPath目录中的文件就重新扫描另1个
        2、至于扫描顺序这里暂且不研究
     */
        while(NULL != (psDirInfo = readdir(pFile))) 
        {
            iRetValue = strlen(psDirInfo -> d_name);
            
            printf("file length = %d,file name is %s\n\r",iRetValue,psDirInfo -> d_name);
        }
    }
    
    
    closedir(pFile);

    return 0;
}
int main()
{

    FileNameScan("./");
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/retry/p/10007818.html