JP29
JP29

Reputation: 625

Using Windows API File functions c++

New to windows programming, there are several examples all over the internet of what i am about to ask however none of them show the comparison which i think is failing.

I'm using several windows api calls throughout my C++ Program and just need some guidence on how to use them correctly.

For example below i have GetFileAttributes() which returns anything from File Attribute Constants.

DWORD dwAttributes = GetFileAttributes(strPathOfFile.c_str());
if ( dwAttributes != 0xffffffff )
{
    if ( dwAttributes == FILE_ATTRIBUTE_NORMAL )
    {
        pkFileInfoList->Add( strPathOfFile + "\t" +"FILE_ATTRIBUTE_NORMAL");
    }
    else if ( dwAttributes == FILE_ATTRIBUTE_ARCHIVE )
    {
        pkFileInfoList->Add( strPathOfFile + "\t" + "FILE_ATTRIBUTE_ARCHIVE");
    }
}

[/CODE]

The if/else statement continues with everything from File Attribute Constants.

Am i using this correctly, i have a directory with over 2500 files which i am feeding the path recusivly. It is always returning FILE_ATTRIBUTE_ARCHIVE.

Thanks,

Upvotes: 0

Views: 662

Answers (1)

Loghorn
Loghorn

Reputation: 2807

GetFileAttributes returns a set of attributes, not a single attribute, so to test correctly you should do:

DWORD dwAttributes = GetFileAttributes(strPathOfFile.c_str());
if ( dwAttributes != 0xffffffff )
{
    if ( dwAttributes & FILE_ATTRIBUTE_NORMAL )
    {
        pkFileInfoList->Add( strPathOfFile + "\t" +"FILE_ATTRIBUTE_NORMAL");
    }
    else if ( dwAttributes & FILE_ATTRIBUTE_ARCHIVE )
    {
        pkFileInfoList->Add( strPathOfFile + "\t" + "FILE_ATTRIBUTE_ARCHIVE");
    }
}

I.e. use bitwise & instead of ==

Upvotes: 3

Related Questions