Linux C++获取文件夹大小

http://www.cnblogs.com/emituofo/p/6225403.html

项目中要计算指定文件夹的大小。
百度查到这篇文章,https://my.oschina.net/Tsybius2014/blog/330628
方法可行,运行正确。

拿到我们的项目中,却遇到一些问题:程序中一些读文件的代码,开始报异常,都不到文件。这些都是以前没有遇到过的问题。
到底是什么情况呢?排查了好久,终于发现使用该文章提供的计算文件夹大小的函数(暂且叫做GetDirectorySize),其中有改变当前目录的代码:

chdir(dir);

我们的项目是多线程的,一个线程调用GetDirectorySize,调用的过程中改变了当前目录,而此时另一个线程使用相对路径去读文件,原来能读到的,现在就读不到了。特别提示chdir改变的是,当前进程(当然包括其下所有线程)的工作目录!!!(具体可以查看线程共享进程的那些资源?

为了去掉GetDirectorySize的副作用,我重新实现了该函数:

复制代码
 1 #include <stdio.h>
 2 #include <sys/stat.h>
 3 #include <sys/types.h>
 4 #include <unistd.h>
 5 #include <stdlib.h>
 6 #include <dirent.h>
 7 #include <string.h>
 8 
 9 //计算某目录所占空间大小(包含本身的4096Byte)
10 long long int GetDirectorySize(char *dir)
11 {
12     DIR *dp;
13     struct dirent *entry;
14     struct stat statbuf;
15     long long int totalSize=0;
16 
17     if ((dp = opendir(dir)) == NULL)
18     {
19         fprintf(stderr, "Cannot open dir: %s\n", dir);
20         return -1; //可能是个文件,或者目录不存在
21     }
22     
23     //先加上自身目录的大小
24     lstat(dir, &statbuf);
25     totalSize+=statbuf.st_size;
26 
27     while ((entry = readdir(dp)) != NULL)
28     {
29         char subdir[256];
30         sprintf(subdir, "%s/%s", dir, entry->d_name);
31         lstat(subdir, &statbuf);
32         
33         if (S_ISDIR(statbuf.st_mode))
34         {
35             if (strcmp(".", entry->d_name) == 0 ||
36                 strcmp("..", entry->d_name) == 0)
37             {
38                 continue;
39             }
40 
41             long long int subDirSize = GetDirectorySize(subdir);
42             totalSize+=subDirSize;
43         }
44         else
45         {
46             totalSize+=statbuf.st_size;
47         }
48     }
49 
50     closedir(dp);    
51     return totalSize;
52 }
53 
54 int main(int argc, char* argv[])
55 {
56     char* dir = argv[1];
57     long long int  totalSize = GetDirectorySize(dir);    
58     printf("totalSize: %lld\n", totalSize);
59 
60     return 0;
61 }

回答: 在Linux下,可以使用dirent.h头文件中的相关函数来遍历文件夹。下面是一个示例代码,可以用来遍历文件夹获取文件名: ```cpp #include <iostream> #include <vector> #include <dirent.h> #include <cstring> void GetFileNames(const std::string& path, std::vector<std::string>& filenames) { DIR *pDir; struct dirent* ptr; if(!(pDir = opendir(path.c_str()))){ std::cout << "Folder doesn't Exist!" << std::endl; return; } while((ptr = readdir(pDir))!=0) { if (strcmp(ptr->d_name, ".") != 0 && strcmp(ptr->d_name, "..") != 0){ filenames.push_back(path + "/" + ptr->d_name); } } closedir(pDir); } int main() { std::string folderPath = "./folder"; std::vector<std::string> filenames; GetFileNames(folderPath, filenames); for(const auto& filename : filenames) { std::cout << filename << std::endl; } return 0; } ``` 这段代码使用了`opendir`函数打开文件夹,然后使用`readdir`函数遍历文件夹中的文件。通过判断文件名是否为"."或".."来排除当前目录和上级目录。最后使用`closedir`函数关闭文件夹。你可以根据自己的需求修改代码中的文件夹路径和处理文件的逻辑。 #### 引用[.reference_title] - *1* *3* [windows/Linux C++遍历文件夹里的所有文件并进行读写](https://blog.csdn.net/baidu_35231778/article/details/113849845)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [LinuxC++ 遍历文件夹下的文件名](https://blog.csdn.net/weixin_38419133/article/details/103561532)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值