C++ 更改列出文件的路径时出现问题

C++ 更改列出文件的路径时出现问题,c++,file,path,size,C++,File,Path,Size,我正在尝试从一个文件夹中获取所有大小相同的文件。问题是我更改了当前目录的路径,它找不到文件的大小。它仅适用于当前目录,即项目所在的目录。如果我的wirte d=opendir(“.”)正在工作,但只是这样,我想更改路径。谢谢 守则: #include <stdio.h> #include <dirent.h> #include <sys/stat.h> void main() { DIR *d; struct dirent *de;

我正在尝试从一个文件夹中获取所有大小相同的文件。问题是我更改了当前目录的路径,它找不到文件的大小。它仅适用于当前目录,即项目所在的目录。如果我的wirte d=opendir(“.”)正在工作,但只是这样,我想更改路径。谢谢 守则:

#include <stdio.h>
#include <dirent.h>
#include <sys/stat.h>

void main()
{
    DIR *d;
    struct dirent *de;
    struct stat buf;
    int exists;
    int total_size;

    d = opendir("C:\\MinGWStudio\\Templates");
    if (d == NULL) {
        perror("prsize");
        exit(1);
    }

    total_size = 0;

    for (de = readdir(d); de != NULL; de = readdir(d)) {
        exists = stat(de->d_name, &buf);
        if (exists < 0) {
            fprintf(stderr, "Couldn't stat %s\n", de->d_name);
        }
        else {
            printf("%s ", de->d_name);
            printf("%d \n", buf.st_size);
            total_size += buf.st_size;
        }
    }
    closedir(d);
    printf("%d\n", total_size);
}
#包括
#包括
#包括
void main()
{
DIR*d;
结构方向*de;
结构统计buf;
int存在;
int总大小;
d=opendir(“C:\\MinGWStudio\\Templates”);
如果(d==NULL){
佩罗尔(“prsize”);
出口(1);
}
总尺寸=0;
for(de=readdir(d);de!=NULL;de=readdir(d)){
exists=stat(de->d_名称和buf);
如果(存在<0){
fprintf(stderr,“无法统计%s\n”,de->d_名称);
}
否则{
printf(“%s”,de->d_名称);
printf(“%d\n”,基本尺寸);
总尺寸+=基本尺寸;
}
}
closedir(d);
printf(“%d\n”,总大小);
}

de->d_name
是没有路径的文件名。您需要提供文件的绝对或相对路径。另一个选择是将当前DIR更改为正在读取的DIR。< /P>我在Windows上工作,在C++中的全局命名空间中不能使用<代码>空隙主()/<代码>。使用
intmain(void)