gen_next
gen_next

Reputation: 97

Checking files size from current directory

Below function read directory and insert files name (by push_back()) into vector

#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);
    }
}

Question: How can I check size of each file (from current directiory) using boots library?

I found method described on http://en.highscore.de/cpp/boost/filesystem.html

It's e.g.:

boost::filesystem::path p("C:\\Windows\\win.ini"); 
std::cout << boost::filesystem::file_size(p) << std::endl; 

Could someone please help how to implement boost it in my open() function? Especially how to assign current directory path name into variable p and then iterate through files names.

Upvotes: 0

Views: 237

Answers (2)

sehe
sehe

Reputation: 393739

Does this help?

Live On Coliru

#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";
    }
}

Note that "." is the current directory

Upvotes: 1

Marchah
Marchah

Reputation: 158

#include <dirent.h>

void open(std::string path){

    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_name);
        std::cout << boost::filesystem::file_size(p) << std::endl;
    }
}

I think it's what you were looking for. You don't need to create a boost::filesystem::path object.

Or you can use the C function stat: http://linux.die.net/man/2/stat

Upvotes: 0

Related Questions