FAT32 Root Directory Read

Hello,

I am trying to read the starting cluster number from a file that i search for. This is found in the 32 byte directory entry in FAT32. My problem is that i cant quite figure out where to get that entry. This is the code i have so far. Note that it is, obviously not doing what i want it to do. Any suggestions?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
static void DisplayFile(string PathLetter) {
    HANDLE hFile;
	DWORD dwBytes2Read;
	DWORD dwFilePointer;
	BYTE byteFileBuff[512];
    FILETIME ftCreation, 
			ftLastaccess, 
			ftLastwrite;
    SYSTEMTIME stCreation, 
			stLastaccess, 
			stLastwrite;
	
	memset(&byteFileBuff, 0, 512);
	wcout<< "Please enter a file with an extension: ";
	string Path = PathLetter + std::string(":\\\\");
	wstring temp;
	temp.assign(Path.begin(),Path.end());
	wstring Filename;
	wcin>> Filename;

	Filename = std::wstring(temp) + Filename;

	hFile = CreateFile(Filename.c_str(), GENERIC_READ,
        FILE_SHARE_READ, 0, OPEN_EXISTING, FILE_ATTRIBUTE_READONLY, 0);
 
    if (hFile==INVALID_HANDLE_VALUE) 
	{
		wcout << L"CreateFile failed for " << Filename.c_str() << endl;
        wcout << "with error code " << GetLastError() << endl;
        return;
    }


  if (GetFileTime(hFile, &ftCreation, &ftLastaccess, &ftLastwrite)) 
  {
		FileTimeToSystemTime(&ftCreation, &stCreation);
        FileTimeToSystemTime(&ftLastaccess, &stLastaccess);
        FileTimeToSystemTime(&ftLastwrite, &stLastwrite);
	wcout << "\nFile Information for: "<< Filename.c_str() << endl;
	WIN32_FIND_DATA FindFileData;
        cout << "Accessed: " << stLastaccess.wYear << "/" << stLastaccess.wMonth<< "/" << stLastaccess.wDay << endl;
		cout << "Size: "<<GetFileSize(hFile, NULL)<<"bytes"<<endl;

	dwFilePointer = SetFilePointer(hFile, 0, NULL, FILE_BEGIN);
		if (dwFilePointer != INVALID_SET_FILE_POINTER)
		{
			if (!ReadFile(hFile, byteFileBuff, 512, &dwBytes2Read, NULL))
				{
					printf("Error in Reading Root Entry.\n");
				}
			else
			{
				BYTE *pRoot = byteFileBuff;
				tdFILE *pFileRecord = (tdFILE *)pRoot;
			}

		}

    } else
        cout << "Error code: " << GetLastError() << endl;
 
    CloseHandle(hFile);
}
Last edited on
There is no windows API to do that, except to open the FAT32 partition itself using CreateFile yourself and have a very good understanding of how FAT32 file system is organized.
And that is exactly what im doing.
And it is far from fun.
Last edited on
I don't think you do it, not in your posted code. There you just open the file.

THe correct way (and more complicated) can be found in MSDN:
http://msdn.microsoft.com/en-us/library/aa363785%28v=VS.85%29.aspx

Detecting the Type of Disk

There is no specific function to programmatically detect the type of disk a particular file or directory is located on. There is an indirect method.

First, call GetVolumePathName. Then, call CreateFile to open the volume using the path. Next, use IOCTL_VOLUME_GET_VOLUME_DISK_EXTENTS with the volume handle to obtain the disk number and use the disk number to construct the disk path, such as "\\?\PhysicalDriveX". Finally, use IOCTL_DISK_GET_DRIVE_LAYOUT_EX to obtain the partition list, and check the PartitionType for each entry in the partition list.
Topic archived. No new replies allowed.