我在子文件夹中读取文件时遇到问题。我在不同的文件夹中有大约 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;
}
}