设为首页 加入收藏

TOP

支持多进程读取同一个目录
2017-10-13 10:22:09 】 浏览:7632
Tags:支持 进程 读取 同一个 目录

分享一个自己用的比较多的读取文件函数

错误返回-1

目录为空返回0,

目录有数返回1,

 

pDir = opendir(cInputPath);

int ReadPath(char *cDestPath, const int *len, const DIR *pDir,
    const char *cInputPath, const char *cWorkPath)
{
    struct dirent *pRd = NULL;
    size_t iCount = 0;
    char cSrcPath[256] = {0};

    while (true) {
        pRd = readdir(pDir);
        if (pRd == NULL) {
            if (errno == EBADF) {
                LOG("ERROR:ReadPath->readdir!\n");
                return -1;
            }

            /* No data */
            rewinddir(pDir);
            return 0;
        } else if (strcmp(pRd->d_name, ".") == 0 ||
                strcmp(pRd->d_name, "..") == 0) {
            continue;
        } else {
            snprintf(cSrcPath, sizeof(cSrcPath), "%s/%s",
                    cInputPath, pRd->d_name);
            snprintf(cDestPath, *len, "%s/%s", cWorkPath,  pRd->d_name);

            if (rename(cSrcPath, cDestPath) < 0) {
                if (errno == ENOENT || errno == EINVAL) {
                    /* Mutil pro rename file */
                    if (++iCount >= 3) {
                        LOG("WARN:ReadPath->rename [%s] to [%s]!\n",
                                    cSrcPath, cDestPath);
                        rewinddir(pDir);
                        iCount = 0;
                    }
                    continue;
                }
                LOG("ERROR:ReadPath->rename [%s] to [%s]!\n",
                            cSrcPath, cDestPath);
                return -1;
            }
            break;
        }
    }
    
    /* Get current file name
    memset(cFileName, 0x00, sizeof(cFileName));
    strcpy(cFileName, pRd->d_name);
    */

    return 1;
}

 

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇c++的构造函数 下一篇C++ Primer 详解(第二章)

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目