我在子文件夹中读取文件时遇到问题。我在差异文件夹中有大约6000个不同的文件。我会读每个文件。但如果我读了大约2000个文件,那么应用程序就没问题了。当我读取6000个文件时,这意味着整个子文件夹。应用程序将显示“无法打开文件”的问题。但如果我只访问未打开的文件夹,那么应用程序不是问题。我不知道会发生什么?我想也许我读了很多文件和内存还不够。你能编辑帮帮我吗?
//This is code to access subforder
static int
find_directory(
const char *dirname)
{
DIR *dir;
char buffer[PATH_MAX + 2];
char *p = buffer;
const char *src;
const char* folder_dir;
char *end = &buffer[PATH_MAX];
int ok;
/* Copy directory name to buffer */
src = dirname;
printf("src=%s\n",src);
while (p < end && *src != '\0') {
*p++ = *src++;
}
*p = '\0';
/* Open directory stream */
dir = opendir (dirname);
if (dir != NULL) {
struct dirent *ent;
/* Print all files and directories within the directory */
while ((ent = readdir (dir)) != NULL) {
char *q = p;
char c;
/* Get final character of directory name */
if (buffer < q) {
c = q[-1];
} else {
c = ':';
}
/* Append directory separator if not already there */
if (c != ':' && c != '/' && c != '\\') {
*q++ = '/';
}
/* Append file name */
src = ent->d_name;
while (q < end && *src != '\0') {
*q++ = *src++;
}
*q = '\0';
/* Decide what to do with the directory entry */
switch (ent->d_type) {
case DT_REG:
/* Output file name with directory */
{
printf ("FILE=%s\n", buffer);
OFBool check= readfile(buffer)
}
break;
case DT_DIR:
/* Scan sub-directory recursively */
if (strcmp (ent->d_name, ".") != 0
&& strcmp (ent->d_name, "..") != 0) {
find_directory (buffer,opts);
}
break;
default:
/* Do not device entries */
/*NOP*/;
}
}
closedir (dir);
ok = 1;
} else {
/* Could not open directory */
printf ("Cannot open directory %s\n", dirname);
ok = 0;
}
return ok;
}
OFBool readfile(const char* filepath)
{
FILE *f=NULL;
OFBool ok = OFFalse;
if( ( f = fopen( filepath, "rb" ) ) == NULL ) // checks to see if file exists
{
ok = OFFalse;
cout<<"can not read file"<<filepath<<endl;
return ok;
}
else
{
ok = true;
cout<<" reading OK"<<endl;
fclose(f);
return ok;
}
}
答案 0 :(得分:0)
这样的事情怎么样:
/*
* Recursively walk though a directory tree.
*
* Arguments:
* path - The root directory to start from
* hidden - If non-zero, include hidden files and directories
*/
void recurse_directory(const char *path, const int hidden)
{
DIR *dir = opendir(path);
if (dir == NULL)
{
perror("opendir");
return;
}
struct dirent *ent;
while ((ent = readdir(dir)) != NULL)
{
if (ent->d_type == DT_DIR)
{
if (strcmp(ent->d_name, ".") != 0 &&
strcmp(ent->d_name, "..") != 0 &&
(ent->d_name[0] != '.' || hidden))
{
char *newpath = malloc(strlen(path) + 1 + strlen(ent->d_name) + 1);
strcpy(newpath, path);
strcat(newpath, "/");
strcat(newpath, ent->d_name);
recurse_directory(newpath, hidden);
free(newpath);
}
}
else if (ent->d_type == DT_REG)
{
if (ent->d_name[0] != '.' || hidden)
{
char *newpath = malloc(strlen(path) + 1 + strlen(ent->d_name) + 1);
strcpy(newpath, path);
strcat(newpath, "/");
strcat(newpath, ent->d_name);
printf("File %s\n", newpath);
/* readfile(newpath); */
free(newpath);
}
}
}
}
答案 1 :(得分:0)
还要添加Joachim Pileborg的答案。如果您正在处理大量子目录,则需要在while循环后添加 closedir()调用。这是因为一次可以打开的目录数量有限制。上面的示例在使用小文件夹时可以正常工作,但在使用2000+子文件夹时,您需要在打开另一个目录之前关闭该目录。
/*
* Recursively walk though a directory tree.
*
* Arguments:
* path - The root directory to start from
* hidden - If non-zero, include hidden files and directories
*/
void recurse_directory(const char *path, const int hidden)
{
DIR *dir = opendir(path);
if (dir == NULL)
{
perror("opendir");
return;
}
struct dirent *ent;
while ((ent = readdir(dir)) != NULL)
{
if (ent->d_type == DT_DIR)
{
if (strcmp(ent->d_name, ".") != 0 &&
strcmp(ent->d_name, "..") != 0 &&
(ent->d_name[0] != '.' || hidden))
{
char *newpath = malloc(strlen(path) + 1 + strlen(ent->d_name) + 1);
strcpy(newpath, path);
strcat(newpath, "/");
strcat(newpath, ent->d_name);
recurse_directory(newpath, hidden);
free(newpath);
}
}
else if (ent->d_type == DT_REG)
{
if (ent->d_name[0] != '.' || hidden)
{
char *newpath = malloc(strlen(path) + 1 + strlen(ent->d_name) + 1);
strcpy(newpath, path);
strcat(newpath, "/");
strcat(newpath, ent->d_name);
printf("File %s\n", newpath);
/* readfile(newpath); */
free(newpath);
}
}
}
closedir(dir); /* Add this <--- */
}