Click here to Skip to main content
15,900,108 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
AnswerRe: Simple Question: Where to store global object? Pin
David Crow12-Jan-09 3:30
David Crow12-Jan-09 3:30 
Questioncompare c++/MFC to C#/.net Pin
Seraph_summer10-Jan-09 5:09
Seraph_summer10-Jan-09 5:09 
AnswerRe: compare c++/MFC to C#/.net Pin
Hamid_RT10-Jan-09 5:52
Hamid_RT10-Jan-09 5:52 
QuestionRe: compare c++/MFC to C#/.net Pin
bob1697210-Jan-09 6:07
bob1697210-Jan-09 6:07 
AnswerRe: compare c++/MFC to C#/.net Pin
Loreia10-Jan-09 6:09
Loreia10-Jan-09 6:09 
AnswerRe: compare c++/MFC to C#/.net Pin
Stuart Dootson10-Jan-09 13:36
professionalStuart Dootson10-Jan-09 13:36 
AnswerRe: compare c++/MFC to C#/.net Pin
Green Fuze11-Jan-09 6:48
Green Fuze11-Jan-09 6:48 
Question::FindFirstFile problem [modified] Pin
Loreia10-Jan-09 4:48
Loreia10-Jan-09 4:48 
I am writing a small article about file enumeration, and in one of my CppUnit test I *discovered* that FindFirstFile/FindNextFile won't correctly report file attributes on FAT32 and FAT16. Specifically, FILE_ATTRIBUTE_TEMPORARY attribute is the problem here:


Here is what my code does:

C++
// first I set desired attribute
SetFileAttributes(sTestFile.c_str(), FILE_ATTRIBUTE_TEMPORARY);

// then I read attributes with GetFileAttributes
DWORD attr_1 = GetFileAttributes(sTestFile.c_str());
// attr_1 is FILE_ATTRIBUTE_TEMPORARY on all three FS tested (NTFS, FAT32 and FAT16)

// then I read attributes with FindFirstFile
FindFirstFile(sTestFile.c_str(), &fd);

// now I get that fd.dwFileAttributes equals FILE_ATTRIBUTE_NORMAL on FAT32 and FAT16, but FILE_ATTRIBUTE_TEMPORARY on NTFS !!!!



It took me nearly a day to realize that problem is due to File system and not my code. And now after spending hours going through on-line documentation I decided to ask here:

1. Is this documented behavior or a bug?
2. Am I doing something wrong in my code. Maybe it is something about temporary files that I failed to take into account.

Any help would be highly appreciated. I have tested this on two computers, both running XP, and I get same results on both.

Finally, here is a simple console project that demonstrates the problem:

C++
#include "tchar.h"
#include "Windows.h"
#include <string>
#include <iostream>

using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
    basic_string<tchar> sPath = _T("");
    basic_string<tchar> sTestFile = _T("\\test_file.txt");
    TCHAR root[256] = {0};
    DWORD size = 256;
    WIN32_FIND_DATA fd;

    if (::GetCurrentDirectory(size, root) != 0)
        sPath = root;
    else return 1;

    sPath += sTestFile;

	CreateFile(
				sPath.c_str(), 
				GENERIC_READ | GENERIC_WRITE, 
				FILE_SHARE_READ, 
				NULL, 
				CREATE_ALWAYS, 
				FILE_ATTRIBUTE_TEMPORARY, 
				NULL);	// creates file with two attributes set: FILE_ATTRIBUTE_ARCHIVE and FILE_ATTRIBUTE_TEMPORARY

    _tprintf(_T("\nA file %s is created...\n"), sPath.c_str());
    _tprintf(_T("\nFile attributes (as reported by GetFileAttributes() ) are now: %x \n"), GetFileAttributes(sPath.c_str()));

    _tprintf(_T("\nExplicitly set FILE_ATTRIBUTE_TEMPORARY attribute...\n"));
    SetFileAttributes(sPath.c_str(), FILE_ATTRIBUTE_TEMPORARY);
    // this line ALWAYS prints: 100 (code for FILE_ATTRIBUTE_TEMPORARY)
    _tprintf(_T("\nFile attributes (as reported by GetFileAttributes() ) are now: %x \n"), GetFileAttributes(sTestFile.c_str()));


    // this is where problems begin !!!
    FindFirstFile(sTestFile.c_str(), &fd);

    // on NTFS this line prints: 100 (code for FILE_ATTRIBUTE_TEMPORARY), which is the same value as reported by GetFileAttributes
    // on FAT32 and FAT16 this line prints: 80 (code for FILE_ATTRIBUTE_NORMAL) !!!!
    _tprintf(_T("\nAccessing file with FindFirstFile()...\n"));
    _tprintf(_T("\nFile attributes (as reported by FindFirstFile() ) are now: %x\n"), fd.dwFileAttributes);

    DeleteFile(sPath.c_str());

    // print attributes for for easier reference
    _tprintf(_T("\n\n// copied from winnt.h\n"));
    _tprintf(_T("//#define FILE_ATTRIBUTE_READONLY             0x00000001  \n"));
    _tprintf(_T("//#define FILE_ATTRIBUTE_HIDDEN               0x00000002  \n"));
    _tprintf(_T("//#define FILE_ATTRIBUTE_SYSTEM               0x00000004  \n"));
    _tprintf(_T("//#define FILE_ATTRIBUTE_DIRECTORY            0x00000010  \n"));
    _tprintf(_T("//#define FILE_ATTRIBUTE_ARCHIVE              0x00000020  \n"));
    _tprintf(_T("//#define FILE_ATTRIBUTE_DEVICE               0x00000040  \n"));
    _tprintf(_T("//#define FILE_ATTRIBUTE_NORMAL               0x00000080  \n"));
    _tprintf(_T("//#define FILE_ATTRIBUTE_TEMPORARY            0x00000100  \n"));
    _tprintf(_T("//#define FILE_ATTRIBUTE_SPARSE_FILE          0x00000200  \n"));
    _tprintf(_T("//#define FILE_ATTRIBUTE_REPARSE_POINT        0x00000400  \n"));
    _tprintf(_T("//#define FILE_ATTRIBUTE_COMPRESSED           0x00000800  \n"));
    _tprintf(_T("//#define FILE_ATTRIBUTE_OFFLINE              0x00001000  \n"));
    _tprintf(_T("//#define FILE_ATTRIBUTE_NOT_CONTENT_INDEXED  0x00002000  \n"));
    _tprintf(_T("//#define FILE_ATTRIBUTE_ENCRYPTED            0x00004000  \n"));


	return 0;
}

</tchar></tchar></iostream></string>


modified on Saturday, January 10, 2009 3:38 PM

AnswerRe: ::FindFirstFile problem Pin
Luc Pattyn10-Jan-09 5:15
sitebuilderLuc Pattyn10-Jan-09 5:15 
GeneralRe: ::FindFirstFile problem Pin
Loreia10-Jan-09 5:52
Loreia10-Jan-09 5:52 
GeneralRe: ::FindFirstFile problem Pin
Luc Pattyn10-Jan-09 6:03
sitebuilderLuc Pattyn10-Jan-09 6:03 
GeneralRe: ::FindFirstFile problem Pin
Loreia10-Jan-09 6:20
Loreia10-Jan-09 6:20 
GeneralRe: ::FindFirstFile problem Pin
Luc Pattyn10-Jan-09 6:46
sitebuilderLuc Pattyn10-Jan-09 6:46 
GeneralRe: ::FindFirstFile problem Pin
Loreia10-Jan-09 7:55
Loreia10-Jan-09 7:55 
GeneralRe: ::FindFirstFile problem Pin
Luc Pattyn10-Jan-09 8:09
sitebuilderLuc Pattyn10-Jan-09 8:09 
GeneralRe: ::FindFirstFile problem Pin
Loreia10-Jan-09 8:33
Loreia10-Jan-09 8:33 
GeneralRe: ::FindFirstFile problem Pin
Randor 10-Jan-09 8:59
professional Randor 10-Jan-09 8:59 
GeneralRe: ::FindFirstFile problem Pin
Loreia10-Jan-09 10:09
Loreia10-Jan-09 10:09 
GeneralRe: ::FindFirstFile problem Pin
Randor 10-Jan-09 10:45
professional Randor 10-Jan-09 10:45 
GeneralRe: ::FindFirstFile problem Pin
Loreia10-Jan-09 10:56
Loreia10-Jan-09 10:56 
GeneralRe: ::FindFirstFile problem Pin
Luc Pattyn10-Jan-09 10:11
sitebuilderLuc Pattyn10-Jan-09 10:11 
QuestionRe: ::FindFirstFile problem Pin
David Crow12-Jan-09 3:36
David Crow12-Jan-09 3:36 
AnswerRe: ::FindFirstFile problem Pin
Loreia12-Jan-09 7:24
Loreia12-Jan-09 7:24 
QuestionCan I handle click events on a static control [SOLVED] Pin
sashoalm10-Jan-09 4:15
sashoalm10-Jan-09 4:15 
AnswerRe: Can I handle click events on a static control Pin
Randor 10-Jan-09 4:35
professional Randor 10-Jan-09 4:35 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.