C++ 正在检查当前目录中的文件大小
下面的函数读取目录并将文件名(通过push_back())插入到向量中C++ 正在检查当前目录中的文件大小,c++,boost,C++,Boost,下面的函数读取目录并将文件名(通过push_back())插入到向量中 #include <dirent.h> void open(string path){ DIR* dir; dirent *pdir; dir = opendir(path.c_str()); while (pdir = readdir(dir)){ vectorForResults.push_back(pdir->d_name); } } #
#include <dirent.h>
void open(string path){
DIR* dir;
dirent *pdir;
dir = opendir(path.c_str());
while (pdir = readdir(dir)){
vectorForResults.push_back(pdir->d_name);
}
}
#包括
无效打开(字符串路径){
DIR*DIR;
dirent*pdir;
dir=opendir(path.c_str());
while(pdir=readdir(dir)){
vectorForResults.push_back(pdir->d_名称);
}
}
问题:如何使用boots库检查每个文件(从当前目录)的大小
我找到了上面描述的方法
例如:
boost::filesystem::path p("C:\\Windows\\win.ini");
std::cout << boost::filesystem::file_size(p) << std::endl;
boost::filesystem::path p(“C:\\Windows\\win.ini”);
std::cout#包括
void open(标准::字符串路径){
DIR*DIR;
dirent*pdir;
dir=opendir(path.c_str());
while(pdir=readdir(dir)){
std::string p=path+“/”+pdir->d_name;
vectorForResults.push_back(pdir->d_名称);
这有帮助吗
#include <boost/filesystem.hpp>
#include <boost/range/iterator_range.hpp>
#include <iostream>
namespace fs = boost::filesystem;
int main()
{
for(auto& f : boost::make_iterator_range(fs::directory_iterator("."), {}))
{
if (fs::is_regular(f))
std::cout << fs::file_size(f) << "\t" << f << "\n";
}
}
#包括
#包括
#包括
名称空间fs=boost::filesystem;
int main()
{
for(auto&f:boost::make_iterator_range(fs::directory_iterator(“.”,{}))
{
if(fs::is_正则(f))
std::cout如果有某种…boost::filesystem::path p(path.c_str())但是如果没有成功……可能的副本可能不会正确使用<代码> PDIR 。它可能指向静态缓冲区,所以确保您正在拷贝它指向的副本。如果<代码> VCCTRESFREST <代码>正在存储指针,那么您就有麻烦了。对不起,我没有测试代码,我在这个计算机上没有C++编译器。当我将第一行更改为:for(auto&f:boost::make_iterator_range(fs::directory_iterator(“.”))时,它就起作用了。非常感谢@sehe!
#include <boost/filesystem.hpp>
#include <boost/range/iterator_range.hpp>
#include <iostream>
namespace fs = boost::filesystem;
int main()
{
for(auto& f : boost::make_iterator_range(fs::directory_iterator("."), {}))
{
if (fs::is_regular(f))
std::cout << fs::file_size(f) << "\t" << f << "\n";
}
}