各位大虾帮忙啊!!怎样用VC++读取windows系统FAT表?

huandieshang 2009-05-03 05:59:46
本人只是编程的初学者,大家说的能详细点么。。。求。。。。
...全文
70 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
wuyu637 2009-05-03
  • 打赏
  • 举报
回复
#include <windows.h>
#include <conio.h>
#include <tchar.h>
#include <winioctl.h>
VOID __cdecl _tmain(
INT Argc,
PTCHAR Argv[]
)
{
TCHAR szName[MAX_PATH] = { 0 };
HANDLE hDisk;
//
// Validate the parameters
//
if (Argc != 3) {
_tprintf(_T("Reads a sector on the disk\n\n"));
_tprintf(_T("%s [disk number] [sector]\n"), Argv[0]);
return;
}
_sntprintf(szName, sizeof(szName) / sizeof(szName[0]) - 1, _T("\\\\.\\Physicaldrive%d"), _ttoi(Argv[1]));
//
// Open a handle to the disk
//
hDisk = CreateFile(szName,
GENERIC_READ,
FILE_SHARE_READ,
NULL,
OPEN_EXISTING,
NULL,
0);
if (hDisk != INVALID_HANDLE_VALUE) {

DISK_GEOMETRY diskGeometry;
DWORD dwBytes;
//
// Obtain the layout of this disk
//
if (DeviceIoControl(hDisk,
IOCTL_DISK_GET_DRIVE_GEOMETRY,
NULL,
0,
&diskGeometry,
sizeof(DISK_GEOMETRY),
&dwBytes,
NULL)) {
DWORD dwSize = diskGeometry.BytesPerSector;
PVOID lpBuffer = new BYTE [dwSize];

if (lpBuffer) {

PARTITION_INFORMATION partitionInfo;
//
// Obtain the size of this disk
//
if (DeviceIoControl(hDisk,
IOCTL_DISK_GET_PARTITION_INFO,
NULL,
0,
&partitionInfo,
sizeof(PARTITION_INFORMATION),
&dwBytes,
NULL)) {
LONGLONG sectorCount = partitionInfo.PartitionLength.QuadPart / diskGeometry.BytesPerSector;
LONGLONG nIndex = _ttoi64(Argv[2]);

_tprintf(_T("Disk %d has 0x%I64x sectors with 0x%x bytes in every sector\n"), _ttoi(Argv[1]), sectorCount, diskGeometry.BytesPerSector);
//
// Read in the requested sector
//
if (nIndex < sectorCount) {
LARGE_INTEGER offset;
offset.QuadPart = (nIndex) * diskGeometry.BytesPerSector;
SetFilePointer(hDisk, offset.LowPart, &offset.HighPart, FILE_BEGIN);
if (ReadFile(hDisk, lpBuffer, dwSize, &dwBytes, NULL))
{

for (ULONG nOffset = 0; nOffset < dwBytes; nOffset += 0x10)
{ ULONG nBytes, nIdx;

_tprintf(_T("%011I64x "), (offset.QuadPart) + nOffset);
//
// Display the data in hexadecimal
//
nBytes = min(0x10, dwBytes - nOffset);

for (nIdx = 0; nIdx < nBytes; nIdx++) {
_tprintf(_T("%02x %s"), ((PUCHAR)lpBuffer)[nOffset + nIdx], ((nIdx + 1) % 0x8) ? _T("") : _T(" "));
}

for ( ; nIdx < 0x10; nIdx++) {
_tprintf(_T(" %s"), ((nIdx + 1) % 0x8) ? _T("") : _T(" "));
}

for (nIdx = 0; nIdx < nBytes; nIdx++) {
_tprintf(_T("%c"), isprint(((PUCHAR)lpBuffer)[nOffset + nIdx]) ? ((PUCHAR)lpBuffer)[nOffset + nIdx] : _T('.'));
}

_tprintf(_T("\n"));
}

} else {
_tprintf(_T("ReadFile() on sector 0x%I64x failed with error code: %d\n"), nIndex, GetLastError());
}

} else {
_tprintf(_T("The requested sector is out-of-bounds\n"));
}

} else {
_tprintf(_T("IOCTL_DISK_GET_PARTITION_INFO failed with error code %d\n"), GetLastError());
}

delete [] lpBuffer;

} else {
_tprintf(_T("Unable to allocate resources, exiting\n"));
}

} else {
_tprintf(_T("IOCTL_DISK_GET_DRIVE_GEOMETRY failed with error code %d\n"), GetLastError());
}
CloseHandle(hDisk);
} else {
_tprintf(_T("CreateFile() on %s failed with error code %d\n"), szName, GetLastError());
}
_tprintf(_T("\n"));
return;
}
Paradin 2009-05-03
  • 打赏
  • 举报
回复
不知。up

65,211

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧