简体   繁体   English

从 c++ 中的磁盘读取数据

[英]Read data from a disk in c++

I'm trying to read some data that lies outside of the partition table, I can successfully read the raw data, but it appears to be encoded in Unicode (UTF-8) or something.我正在尝试读取分区表之外的一些数据,我可以成功读取原始数据,但它似乎以 Unicode (UTF-8) 或其他方式编码。 There is an application that has been written to read this data and display it properly so I know it can be done.已经编写了一个应用程序来读取这些数据并正确显示它,所以我知道它可以完成。

This data is called "Image Safe Data" and it is something Novell ZenWorks places on the disk at the sixth sector (0x05).此数据称为“图像安全数据”,它是 Novell ZenWorks 放置在磁盘第六扇区 (0x05) 上的数据。

The raw data looks like this:原始数据如下所示:

ZISD♂   Æ☻      ☺   └¿ ├└¿ ☺    ñ3åG    ╓ï╝Y    ≡   ♣   §   ╗ç9%¥⌂+0Kâ¬ê


  :☺               @   f 8 b 3 4 6 6 2 9 3 b 3 2 b b f d c 8 c b d 6 2 2 1 2 1 0 d
2 1 ♫   M H S T R E E ▬   F H I F L 0 0 0 5 9 3     L   \ \ F H 0 1 F S N T H 0
9 \ A P P S \ i m g s \ C X P P N 6 7 1 0 B . z m g $   t r i n i t y - h e a l
 t h . o r g ▬   F H I F L 0 0 0 5 9 3 H   Z N W

I wrote the following code to read the data directly from disk in C++:我编写了以下代码直接从 C++ 中的磁盘读取数据:

#include <iostream>
#include <windows.h>


void main() {
    DWORD nRead;
    char buf[512];

    HANDLE hDisk = CreateFile("\\\\.\\PhysicalDrive0", 
        GENERIC_READ, FILE_SHARE_READ,        
        NULL, OPEN_EXISTING, 0, NULL);

    SetFilePointer(hDisk, 0xA00, 0, FILE_BEGIN);
    ReadFile(hDisk, buf, 512, &nRead, NULL);
    for (int currentpos=0;currentpos < 512;currentpos++) {
        std::cout << buf[currentpos];
    }
    CloseHandle(hDisk);
}

I'm a novice in C++.我是 C++ 的新手。 I need to find a way to output this data so that it can be read by a script if possible.我需要找到一种方法来 output 这个数据,以便如果可能的话它可以被脚本读取。 It looks to be delimited by inconsistent characters.它看起来是由不一致的字符分隔的。

When you print it you're interpreting the data as a char .当您打印它时,您将数据解释为char Some of the values are in the a-zA-Z "normal, printable" range and so print as you'd expect.一些值在 a-zA-Z“正常,可打印”范围内,因此按您的预期打印。 Others, like '0' are special and unprintable directly.其他的,比如'0'是特殊的,不能直接打印。 You probably want to print the bytes as hex, not characters.您可能希望将字节打印为十六进制,而不是字符。 To do that:要做到这一点:

std::cout << std::hex << std::setw(2) << std::setfill('0') << unsigned(buf[currentpos]) << " ";

This tells the ostream to print two digits of hex, using 0 if there's less and then forces the char to be printed as an unsigned int.这告诉ostream打印两位十六进制数,如果少则使用 0,然后强制将 char 打印为无符号整数。

(If you're interested in learning more about C++ you could also use std::copy and std::ostream_iterator to avoid writing the loop yourself) (如果您有兴趣了解有关 C++ 的更多信息,您还可以使用std::copystd::ostream_iterator来避免自己编写循环)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM