分享一個自己用的比較多的讀取文件函數
錯誤返回-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; }