gpt4 book ai didi

c - 如何在 c 中执行多个读/写 HDD/USB 字节

转载 作者:太空宇宙 更新时间:2023-11-04 06:54:45 25 4
gpt4 key购买 nike

<分区>

首先,这是我看过的几个链接...

Read and write hard disk sector directly and efficiently

Read specific sector on hard drive using C language on windows

我正在尝试做几乎相同的事情。我遇到的问题是多次读取设备,因此我可以存储从设备 (USB) 读取的字节以将它们写入文件。

这就是我想要做的...

  1. 声明变量
  2. 初始化变量
  3. 设置文件指针()
  4. 读取文件()
  5. 将读取的字节输出到文件
  6. 使用 ReadFile() 获取更多字节
  7. 再次将读取的字节输出到同一个文件
  8. 重复6和7(实际上只是重复4和5)

这似乎不起作用。我想读取 x 个字节并将这些值存储到一个文件中,然后读取更多并将这些值存储在与上次相同的文件中。我希望它重复这个过程,直到它读取到设备的末尾。我希望这个程序可以在任何尺寸的设备上运行。如果我可以将它放在一个循环中,那么我就可以读取和写入无限大小的设备。

这是一个如何读/写的方法,所以我也想反过来做。读取文件中的值,然后将它们写入设备。

我使用的是 128MB USB。它包含 131858432 个字节。如果需要更多信息,我会发布。

我的代码:

#include <windows.h>
#include <stdio.h>

int main(int argc, char ** argv)
{
BYTE sector[0x400] = {0};
DWORD bytesRead;
HANDLE device = NULL;
int numSector = 1;
int maxRead = 1;
FILE *readChar = fopen("G:\\usb_128MB_Dec2.txt", "w+");

device = CreateFile("\\\\.\\L:", // Drive to open
GENERIC_READ|GENERIC_WRITE, // Access mode
FILE_SHARE_READ|FILE_SHARE_WRITE, // Share Mode
NULL, // Security Descriptor
OPEN_EXISTING, // How to create
0, // File attributes
NULL); // Handle to template

if(device == INVALID_HANDLE_VALUE)
{
printf("CreateFile: %u\n", GetLastError());
system("pause");
return 1;
}

// set the file pointer for first time
SetFilePointer(device, numSector, NULL, FILE_BEGIN);

// Edit 1. Comment 2.
if(GetLastError() != NO_ERROR)
{
printf("GetLastError: %d\n", GetLastError());
goto end; // end: is before closing files or handles at end of main()
}

// read device for maxRead number of bytes and store the reading into a file
ReadFile(device, sector, maxRead, &bytesRead, NULL);
fprintf(readChar, "%d\n", sector[0]);

// This part of code does not act like expected
SetFilePointer(device, numSector, NULL, FILE_CURRENT);
if(!ReadFile(device, sector, maxRead, &bytesRead, NULL))
printf("err\n");
else
fprintf(readChar, "%d", sector[0]);

end: // Edit 1. Comment 2.
CloseHandle(device);
fclose(readChar);
system("pause");
return 0;
}

屏幕输出:

GetLastError: 87

文件输出:

Nothing is in the file.

该文件应包含一个 8 位十进制值,而不是空值或 0。

编辑 1:

87 means INVALID PARAMETER.

Your reads have to be sector aligned so you can't seek to offset 1,but only 0, sector_size, 2sector_size, ..., nsector_size, Your readshave also be in multiplies of sector size, and your memory buffer hasto be aligned to sector_size.

Sector size could be retrieved with GetDiskFreeSpace and alignedmemory can be obtained with VirtualAlloc.

Maybe you should check also FSCTL_LOCK_VOLUME andFSCTL_DISMOUNT_VOLUME.

这是用于一次性读取设备的其他代码

#include <windows.h>
#include <stdio.h>

int main(int argc, char ** argv)
{
BYTE sector[0x200] = {0};
DWORD bytesRead;
HANDLE device = NULL;
int numSector = 1;
int maxRead = 0x200;
long long int i;
FILE *readChar = fopen("G:\\usb_128MB_Dec.txt", "w+");

device = CreateFile("\\\\.\\L:", // Drive to open
GENERIC_READ|GENERIC_WRITE, // Access mode
FILE_SHARE_READ|FILE_SHARE_WRITE, // Share Mode
NULL, // Security Descriptor
OPEN_EXISTING, // How to create
0, // File attributes
NULL); // Handle to template

if(device == INVALID_HANDLE_VALUE)
{
printf("CreateFile: %u\n", GetLastError());
system("pause");
return 1;
}

SetFilePointer(device, numSector, NULL, FILE_BEGIN);

if (!ReadFile(device, sector, maxRead, &bytesRead, NULL))
{
printf("ReadFile: %u\n", GetLastError());
goto end;
}
else
{
printf("Success!\n");
}

if(readChar == NULL)
{
printf("Did not open file. Exit 2.");
goto end;
}

for(i = 0; i < maxRead - 1; i++)
{
fprintf(readChar, "%d\n", sector[i]);
}
fprintf(readChar, "%d", sector[i]); // so the previous loop wont add \n to the last read wanted

end:
CloseHandle(device);
fclose(readChar);
system("pause");
return 0;
}

文件内容:

235
88
...

读取的每个字节都以十进制值形式存储在新行上。

所以它可能会更好地理解我正在尝试做的事情,这里是代码:

//  What I want to do..
// This part works
SetFilePointer(device, numSector, NULL, FILE_BEGIN);
ReadFile(device, sector, maxRead, &bytesRead, NULL);

for(i = 0; i < size_of_device - 0x200; i += 512)
{
for(j = 0; j < maxRead; j++)
{
fprintf(readChar, "%d\n", sector[j]);
}

// stops working
SetFilePointer(device, numSector, NULL, FILE_CURRENT);
ReadFile(device, sector, maxRead, &bytesRead, NULL);
}

for(j = 0; j < maxRead - 1; j++)
{
fprintf(readChar, "%d\n", sector[j]);
}
fprintf(readChar, "%d", sector[j]);
// .. end of what i want to do

编辑 2:现在开始多次阅读。

#include <windows.h>
#include <stdio.h>

int main(int argc, char ** argv)
{
BYTE sector[0x200] = {0};
DWORD bytesRead;
HANDLE device = NULL;
//int numSector = 512; // original value was 1 not 512 but variable is not needed
int maxRead = 512;
int i, j, k = 0, l; // loop variables

FILE *readChar = fopen("G:\\wii u hdd image\\usb_128MB_Dec3.txt", "w+");

if(readChar == NULL)
{
printf("Error creating file.\n");
goto end;
}

device = CreateFile("\\\\.\\L:", // Drive to open
GENERIC_READ|GENERIC_WRITE, // Access mode
FILE_SHARE_READ|FILE_SHARE_WRITE, // Share Mode
NULL, // Security Descriptor
OPEN_EXISTING, // How to create
0, // File attributes
NULL); // Handle to template

// If device does not contain a handle value
if(device == INVALID_HANDLE_VALUE)
{
printf("Error. GetLastError: %u\n", GetLastError());
goto end;
}

for(i = 0; i < maxRead*503; i++) // maxRead * 503 = 257536
{
// If ReadFile() fails it will exit the program without adding a '\n' to the readChar file.
if(!ReadFile(device, sector, maxRead, &bytesRead, NULL))
{
printf("Error of ReadFile(). GetLastError(): %u\n", GetLastError());
goto end;
}

// If this is the first time through the loop then '\n' won't be added to the readChar file.
if(i != 0)
{
fprintf(readChar, "\n");
system("cls");
printf("%.2f%%\n", (i / 257536));
}

// Runs for 511 times. Then prints the 512th decimal value after the loop.
for(j = 0; j < maxRead - 1; j++)
{
fprintf(readChar, "%d\n", sector[j]);
}
fprintf(readChar, "%d", sector[j]);
}

end:
CloseHandle(device);
fclose(readChar);
system("pause");
return 0;
}

编辑 3:

这个问题在其他帖子中没有得到回答。

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com