这是我的应用程序代码的一部分我正在移植到最初在Ubuntu上编写的windowsXp。
#include <stdio.h>
#include <string.h>
#include <dirent.h>
#include <unistd.h>
#include <sys/stat.h>
int list_files(char *parentdir)
{
DIR *dir;
struct dirent *de = NULL;
char subdirs[1000][1000];
int isubdirs = 0;
char rootdir[1000];
strcpy(rootdir, parentdir);
FILE *fp = fopen("list.txt", "w");
seek:
dir = opendir(rootdir);
if (!dir) {
printf("ERROR: [ %s ]", rootdir);
perror("Couldn't opendir: ");
}
else {
de = readdir(dir);
if (!de) {
printf("ERROR:[ %s ]", rootdir);
perror("Couldn't readdir ");
goto out;
}
if (strcmp(de->d_name, "..") != 0 && strcmp(de->d_name, ".") != 0) {
fprintf(fp, "%s\\%s\n", rootdir, de->d_name);
printf("ADDED: %s\\%s\n", rootdir, de->d_name);
}
while ((de = readdir(dir)) != NULL)
{
if (!strcmp(de->d_name, "..") || !strcmp(de->d_name, "."))
continue;
fprintf(fp, "%s\\%s\n", rootdir, de->d_name);
printf("ADDED: %s\\%s\n", rootdir, de->d_name);
struct stat s;
stat(de->d_name, &s);
if (S_ISDIR(s.st_mode))
{
strcpy(subdirs[isubdirs], rootdir);
strcat(subdirs[isubdirs], "\\");
strcat(subdirs[isubdirs], de->d_name);
printf("%s\n", subdirs[isubdirs]);
isubdirs++;
}
/* Not working in WinXP
if (de->d_type == 4) {
strcpy(subdirs[isubdirs], rootdir);
strcat(subdirs[isubdirs], "/");
strcat(subdirs[isubdirs], de->d_name);
isubdirs++;
}
*/
}
if (isubdirs > 0) {
strcpy(rootdir, subdirs[--isubdirs]);
goto seek;
}
}
out:
fclose(fp);
return 0;
}
现在问题出在我得到的输出文件中:
C:\Documents and Settings\user\Desktop\New Folder\00.txt
C:\Documents and Settings\user\Desktop\New Folder\1
C:\Documents and Settings\user\Desktop\New Folder\2
仅列出parentdir中的文件,但不在子目录中(1和2都不为空“文件夹”) 我在Virtualbox中测试。