Panel użytkownika
Nazwa użytkownika:
Hasło:
Nie masz jeszcze konta?

fstream i ilosc plikow w folderze

Ostatnio zmodyfikowano 2012-12-30 13:37
Autor Wiadomość
kizia
Temat założony przez niniejszego użytkownika
fstream i ilosc plikow w folderze
» 2012-12-29 11:26:39
Czy jest mozliwosc sprawdzenia ilosci plikow w folderze za pomoca tej biblioteki?
P-72247
Savail
» 2012-12-29 15:46:31
W standardowych bibliotekach C++ nie ma chyba takiej funkcjonalności. Najlepszym wyjściem byłoby użycie dirent.h lub Boost::filesystem
P-72291
Mrovqa
» 2012-12-29 18:11:08
Wygrzebałem stary kod z mojej DLLki, może Ci się przyda. Napisane w WinAPI, więc windows-only.
FileListingA:
C/C++
#pragma once
//#include "dll.h" // to Ci nie jest potrzebne :)
#include <string>
#include <vector>

#define FL_OK 0
#define FL_CANNOT_FIND 1

extern "C++"
{
    class FileListingA;
}

class /*DLLIMPORT*/ FileListingA
{
public:
    std::vector < std::string > files;
    std::vector < std::string > catalogs;
   
    void ClearLists( void ) { files.clear(); catalogs.clear(); }
    int UpdateFileList( std::string dir, bool ListInSubfolders = true, std::string PrePathAdd = "" );
    int AddToFileList( std::string dir, bool ListInSubfolders = true, std::string PrePathAdd = "" );
   
    FileListingA();
    FileListingA( std::string dir, bool ListInSubfolders = true, std::string PrePathAdd = "" )
    {
        AddToFileList( dir, ListInSubfolders, PrePathAdd );
    }
};
FileListingA.cpp
C/C++
#include "FileListingA.h"
#include "Windows.h"

#undef FindFirstFile
#undef FindNextFile

#define WIN32_FIND_DATA WIN32_FIND_DATAA
#define FindFirstFile FindFirstFileA
#define FindNextFile FindNextFileA

int FileListingA::UpdateFileList( std::string dir, bool ListInSubfolders, std::string PrePathAdd )
{
    files.clear();
    catalogs.clear();
    if( dir.rfind( '/' ) != std::string::npos ) dir[ dir.rfind( '/' ) ] = '\\';
   
    if( dir.length() < 4 || dir.substr( dir.length() - 4, 4 ) != "\\*.*" ) dir += "\\*.*";
   
    if( !PrePathAdd.empty() )
         if( PrePathAdd[ PrePathAdd.length() - 1 ] == '/' ) PrePathAdd[ PrePathAdd.length() - 1 ] = '\\';
    else if( PrePathAdd[ PrePathAdd.length() - 1 ] != '\\' ) PrePathAdd += '\\';
   
    WIN32_FIND_DATA wfd;
    HANDLE hFile = FindFirstFile( dir.c_str(), & wfd );
    if( hFile == INVALID_HANDLE_VALUE ) return FL_CANNOT_FIND;
   
    if( FindNextFile( hFile, & wfd ) == NULL ) { FindClose( hFile ); return FL_CANNOT_FIND; }
    /* if( wfd.dwFileAttributes != FILE_ATTRIBUTE_DIRECTORY )
    files.push_back( PrePathAdd + wfd.cFileName );
    else
    catalogs.push_back( PrePathAdd + wfd.cFileName );*/
   
    while( FindNextFile( hFile, & wfd ) != NULL )
    if( wfd.dwFileAttributes != FILE_ATTRIBUTE_DIRECTORY )
         files.push_back( PrePathAdd + wfd.cFileName );
    else
         catalogs.push_back( PrePathAdd + wfd.cFileName );
   
    FindClose( hFile );
   
    if( ListInSubfolders )
    {
        std::string wdir( dir );
        std::string catalog;
        unsigned int PPALen = PrePathAdd.length(); // PrePathAddLen
        for( unsigned int i = 0; i < catalogs.size(); i++ )
        {
            catalog = catalogs[ i ].substr( PPALen, std::string::npos );
            wdir = dir.substr( 0, dir.length() - 4 ) + '\\' + catalog + "\\*.*";
           
            hFile = FindFirstFile( wdir.c_str(), & wfd );
            if( hFile == INVALID_HANDLE_VALUE ) continue;
           
            if( FindNextFile( hFile, & wfd ) == NULL ) { FindClose( hFile ); continue; }
           
            while( FindNextFile( hFile, & wfd ) != NULL )
            if( wfd.dwFileAttributes != FILE_ATTRIBUTE_DIRECTORY )
                 files.push_back( PrePathAdd + catalog + '\\' + wfd.cFileName );
            else
                 catalogs.push_back( PrePathAdd + catalog + '\\' + wfd.cFileName );
           
            FindClose( hFile );
        }
    }
   
    return FL_OK;
}
int FileListingA::AddToFileList( std::string dir, bool ListInSubfolders, std::string PrePathAdd )
{
    unsigned int catalogsSize = catalogs.size();
    if( dir.rfind( '/' ) != std::string::npos ) dir[ dir.rfind( '/' ) ] = '\\';
   
    if( dir.length() < 4 || dir.substr( dir.length() - 4, 4 ) != "\\*.*" ) dir += "\\*.*";
   
    if( !PrePathAdd.empty() )
         if( PrePathAdd[ PrePathAdd.length() - 1 ] == '/' ) PrePathAdd[ PrePathAdd.length() - 1 ] = '\\';
    else if( PrePathAdd[ PrePathAdd.length() - 1 ] != '\\' ) PrePathAdd += '\\';
   
    WIN32_FIND_DATA wfd;
    HANDLE hFile = FindFirstFile( dir.c_str(), & wfd );
    if( hFile == INVALID_HANDLE_VALUE ) return FL_CANNOT_FIND;
   
    if( FindNextFile( hFile, & wfd ) == NULL ) { FindClose( hFile ); return FL_CANNOT_FIND; }
    /* if( wfd.dwFileAttributes != FILE_ATTRIBUTE_DIRECTORY )
    files.push_back( PrePathAdd + wfd.cFileName );
    else
    catalogs.push_back( PrePathAdd + wfd.cFileName );*/
   
    while( FindNextFile( hFile, & wfd ) != NULL )
    if( wfd.dwFileAttributes != FILE_ATTRIBUTE_DIRECTORY )
         files.push_back( PrePathAdd + wfd.cFileName );
    else
         catalogs.push_back( PrePathAdd + wfd.cFileName );
   
    FindClose( hFile );
   
    if( ListInSubfolders )
    {
        std::string wdir( dir );
        std::string catalog;
        unsigned int PPALen = PrePathAdd.length(); // PrePathAddLen
        for( unsigned int i = catalogsSize; i < catalogs.size(); i++ )
        {
            catalog = catalogs[ i ].substr( PPALen, std::string::npos );
            wdir = dir.substr( 0, dir.length() - 4 ) + '\\' + catalog + "\\*.*";
           
            hFile = FindFirstFile( wdir.c_str(), & wfd );
            if( hFile == INVALID_HANDLE_VALUE ) continue;
           
            if( FindNextFile( hFile, & wfd ) == NULL ) { FindClose( hFile ); continue; }
           
            while( FindNextFile( hFile, & wfd ) != NULL )
            if( wfd.dwFileAttributes != FILE_ATTRIBUTE_DIRECTORY )
                 files.push_back( PrePathAdd + catalog + '\\' + wfd.cFileName );
            else
                 catalogs.push_back( PrePathAdd + catalog + '\\' + wfd.cFileName );
           
            FindClose( hFile );
        }
    }
   
    return FL_OK;
}

/*void FileListingA::ClearLists( void )
{
files.clear();
catalogs.clear();
}*/
Jeżeli chcesz mam też wersję działającą na Unicode. Użycia się domyślisz po pliku nagłówkowym. Dodam, że to
std::string PrePathAdd
 oznacza ciąg znaków, jaki metoda dodaje przed każdą ścieżką.
P-72302
kizia
Temat założony przez niniejszego użytkownika
» 2012-12-30 13:37:45
W padlem na inny pomysl, bo w moim przypadku pliki w danym folderze ponumerowane sa od 0 do n dlatego zrobilem to tak ze otwieram plik pokolei, a nastepnie sprawdzam czy zostal otworzony i tak w kolko az, sie nie uda otworzyc i mam liczbe plikow w folderze :P

Wielkie dzięki za odpowiedzi!
P-72369
« 1 »
  Strona 1 z 1