读书人

文件遍历操作的编译异常帮忙指正一下

发布时间: 2012-03-08 13:30:13 作者: rapoo

文件遍历操作的编译错误,帮忙指正一下错误和改进方法

C/C++ code
void recu_list(const char *path, const FILE *fout) {    if(fout==NULL){        exit(-1);    }    //indent为输出时的缩进    int indent = 2;    struct dirent* ent = NULL;    DIR *pDir;    char dir[512];    struct stat statbuf;    if ((pDir = opendir(path)) == NULL) {        fprintf(stderr, "Cannot open directory:%s\n", path);        return;    }    while ((ent = readdir(pDir)) != NULL) {        //得到读取文件的绝对路径名        snprintf(dir, 512, "%s/%s", path, ent->d_name);        //得到文件信息        lstat(dir, &statbuf);        //判断是目录还是文件        if (S_ISDIR(statbuf.st_mode)) {            //排除当前目录和上级目录            if (strcmp(".", ent->d_name) == 0 || strcmp("..", ent->d_name) == 0)                continue;            //如果是子目录,递归调用函数本身,实现子目录中文件遍历            printf("%*s子目录:%s/\n", indent, "", ent->d_name);            //递归调用,遍历子目录中文件            recu_list(dir, fout);        } else {            printf("%*s文件:%s\n", indent, "", ent->d_name);            fprintf(fout, "%s %s\n", ent->d_name);        }    }//while    closedir(pDir);}


编译提示error: initializing argument 1 of ‘int fprintf(FILE*, const char*, ...)’
我应该怎么改才对呢,文件指针是参数传入的

[解决办法]
const FILE *fout
const去掉
[解决办法]
system("dir /b /a-d c:\\*.* >d:\\allfiles.txt");
//读文件d:\\allfiles.txt的内容即C:\\下所有文件的名字
system("dir /b /ad c:\\*.* >d:\\alldirs.txt");
//读文件d:\\alldirs.txt的内容即C:\\下所有子目录的名字
请记住,能用shell命令获取文件、文件夹信息或者操作文件、文件夹最好用shell命令获取或者操作,而不要用各种API获取或者操作,因为当遇到非法文件夹名或非法文件名或非法文件长度、非法文件日期、压缩文件、链接文件、稀疏文件……等各种意料之外的情况时,API会处理的不全面或陷入死循环,而shell命令不会。

[解决办法]
const问题?

读书人网 >C语言

热点推荐