我在讀取子文件夾中的文件時遇到問題。我在差異文件夾中有大約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;
}
}
這是很多代碼。你可以縮小很多。 – 2013-03-06 13:38:12
你爲什麼要實現自己的'strcpy'和'strcat'? – 2013-03-06 13:48:07
更好的是,這個C++如何? – Angew 2013-03-06 13:49:52