How to obtain direct access to raw HD data in C?

前端 未结 2 1019
挽巷
挽巷 2021-01-06 03:52

I need to do a complete format on a USB stick (FAT16 or FAT32), put a file on the drive, then delete it and recover the file using a C program.

Could anyone give me

2条回答
  •  鱼传尺愫
    2021-01-06 04:51

    This is highly operating system specific.

    For Linux, you would open the raw device /dev/sdxx. Note that there are privilege hoops to manage.

    For Windows, you would use something like:

     HANDLE h = CreateFile ("\\\\.\\PhysicalDriveX", GENERIC_READ,
                        FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
                        OPEN_EXISTING,
                        FILE_FLAG_NO_BUFFERING | FILE_FLAG_RANDOM_ACCESS,
                        NULL);
    

    where X depends on the device.

提交回复
热议问题