Go back to Richel Bilderbeek's homepage.

Go back to Richel Bilderbeek's C++ page.

 

 

 

 

 

(C++) Boost Boost.Filesystem example 1: count C++ webpages

 

This Boost.Filesystem example shows how to count the number of files in a folder with certain requirements. This program shows how I count the number of C++ webpages on this site!

 

 

 

 

 

 

 

 

 

 

 

Technical facts

 

Operating system(s) or programming environment(s)

IDE(s):

Project type:

C++ standard:

Compiler(s):

Libraries used:

 

 

 

 

 

Qt project file: CppFilesystemExample1.pro

 

QT       += core
QT       -= gui
CONFIG   += console
CONFIG   -= app_bundle
TEMPLATE = app

SOURCES += main.cpp

unix {
  LIBS += -lboost_system -lboost_filesystem
}

win32 {
  INCLUDEPATH += \
    ../../Libraries/boost_1_53_0

  #Boost.System
  HEADERS += \
    ../../Libraries/boost_1_53_0/libs/system/src/local_free_on_destruction.hpp
  SOURCES += \
    ../../Libraries/boost_1_53_0/libs/system/src/error_code.cpp

  #Boost.Filesystem
  HEADERS += \
    ../../Libraries/boost_1_53_0/libs/filesystem/src/windows_file_codecvt.hpp
  SOURCES += \
    ../../Libraries/boost_1_53_0/libs/filesystem/src/codecvt_error_category.cpp \
    ../../Libraries/boost_1_53_0/libs/filesystem/src/operations.cpp \
    ../../Libraries/boost_1_53_0/libs/filesystem/src/path.cpp \
    ../../Libraries/boost_1_53_0/libs/filesystem/src/path_traits.cpp \
    ../../Libraries/boost_1_53_0/libs/filesystem/src/portability.cpp \
    ../../Libraries/boost_1_53_0/libs/filesystem/src/unique_path.cpp \
    ../../Libraries/boost_1_53_0/libs/filesystem/src/utf8_codecvt_facet.cpp \
    ../../Libraries/boost_1_53_0/libs/filesystem/src/windows_file_codecvt.cpp
}

 

 

 

 

 

main.cpp

 

#include <cassert>
#include <iostream>
#include <string>
#include <boost/filesystem.hpp>
#include <boost/foreach.hpp>
#include <boost/timer.hpp>

int main()
{
  const std::string my_folder_name = "/home/richel/Projects/Projects/RichelbilderbeekNl";
  const int min_filesize = 1000; //in bytes

  const boost::filesystem::path my_folder
    = boost::filesystem::system_complete(
        boost::filesystem::path(my_folder_name ));

  assert(boost::filesystem::is_directory(my_folder)
    && "The folder must exist");

  int n = 0;

  const boost::filesystem::directory_iterator j;
  for ( boost::filesystem::directory_iterator i(my_folder);
        i != j;
        ++i)
  {
    if ( boost::filesystem::is_regular_file( i->status() ) )
    {
      const std::string filename = i->path().filename().string();
      const std::string full_filename = my_folder_name + "/" + filename;
      const int filesize = boost::filesystem::file_size(full_filename);
      if (filename.size() >= 4
        && filename.substr(filename.size() - 4, 4) == std::string(".htm")
        && filename.substr(0, 3) == std::string("Cpp")
        && filesize > min_filesize)
      {
        ++n;
        std::cout << "(" << filesize << ") " << filename << '\n';
      }
    }
  }
  std::cout
    << "\nNumber of Cpp***.htm files bigger than "
    << min_filesize << " bytes: " << n << '\n';
}

 

 

 

 

 

crosscompiletowindows.sh

 

#!/bin/sh
#From http://richelbilderbeek.nl/CppQtCrosscompileToWindowsExample15.htm

echo "Cross compiling to Windows"

echo "1/2: Creating Windows makefile"
i686-pc-mingw32-qmake CppFilesystemExample1.pro

echo "2/2: making makefile"
make

echo "Done"

 

 

 

 

 

Go back to Richel Bilderbeek's C++ page.

Go back to Richel Bilderbeek's homepage.

 

Valid XHTML 1.0 Strict

This page has been created by the tool CodeToHtml