Frage

I'm trying to figure out the size of a file using the code provided and pass the result into my file object. It will only pass in the file size if it's of a certain extension, this function works perfectly, the only problem that I'm having is that the file size that the ifile.tellg() is passing in -1 for every filesize.

void NonRecursiveProcess(string directory)
{
    for(directory_iterator dir_end, dir(directory); dir != dir_end; ++dir) 
    { 
        path _path(*dir); 
        if(!is_directory(_path)) 
        { 
            for ( vector<ExtensionAbstract*>::iterator it = extensionsAbstract.begin() ; it != extensionsAbstract.end(); ++it)
            {
                if ( _path.file_string().substr(_path.file_string().find_last_of(".") + 1) == (*it)->GetExtension())
                {
                    std::ifstream ifile(_path.file_string(), std::ifstream::in | std::ifstream::binary );
                    ifile.seekg(0, std::ios_base::end);
                    (*it)->AddkB(ifile.tellg());
                    (*it)->AddFileCount();
                }
            }
        } 
    } 
}`
War es hilfreich?

Lösung

istream::tellg() returns -1 if ifstream is in the failed state. fail may be set in seekg() or, much more probably, in the constructor -- check for the ifile state after the construction to see whether it is actually opened. You could get access denied, bugs in the name passing or something else.

One possible source of error: isn't _path.file_string() returns only file name without absolute path? If so, ifile constructor could give you "file not found".

Otherwise, the combination of ifile.seekg(0,std::ios_base::end); and ifile.tellg(); should work as intended on any binary file, giving the file size.

Andere Tipps

Perhaps stat would be useful (I know it is not standard).

Anyway this should be the signature of the function

void NonRecursiveProcess(const string &directory)

Will help in the performance of your program.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top