支持多进程读取同一个目录

2018-06-17 23:31:53来源:未知 阅读 ()

新老客户大回馈,云服务器低至5折

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

错误返回-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;
}

 

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:当年只会C# 所以写C++就成这样了! log4cplus -&gt; log4net

下一篇:C++里创建 Trie字典树(中文词典)(三)(联想)