在 Linux 上用 C++ 读取硬盘扇区
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7289453/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Reading Hard Disk Sectors in C++ on Linux
提问by Anthoni C
How do you read hard disk sectors in C++ with gcc/linux? Is there a standard library that I can use or must something be downloaded? In Windows I can use CreateFile(...) to access raw disk sectors, but I do not know how to do in in Linux.
你如何用gcc/linux在C++中读取硬盘扇区?是否有我可以使用的标准库或必须下载某些内容?在 Windows 中,我可以使用 CreateFile(...) 访问原始磁盘扇区,但我不知道在 Linux 中如何操作。
I am using GCC on Ubuntu LTS 10.4. Thank you for your help.
我在 Ubuntu LTS 10.4 上使用 GCC。感谢您的帮助。
采纳答案by Dietrich Epp
The hard disk is just another file (not a "regular file" but a "device file", but still, a file). Open it the normal way...
硬盘只是另一个文件(不是“普通文件”而是“设备文件”,但仍然是一个文件)。正常打开...
int fdes = open("/dev/sda1", O_RDONLY);
if (fdes < 0)
err(1, "/dev/sda1");
... do more ...
You will get permission errors unless you have the right permissions. Note that "/dev/sda1"
is just an example, it is the first partition on disk sda
, the exact path will depend on your system. You can list mount points with the mount
command, and you can access entire disks (instead of just partitions) using /dev/sda
, /dev/sdb
, etc.
除非您拥有正确的权限,否则您将收到权限错误。请注意,这"/dev/sda1"
只是一个示例,它是磁盘上的第一个分区sda
,确切的路径将取决于您的系统。你可以列出与挂载点mount
命令,你可以使用访问整个磁盘(而不仅仅是分区)/dev/sda
,/dev/sdb
等等。
You could also open it as a C++ fstream
or C FILE
, but I do not recommend this. You will have a better time finding example code and getting help on forums if you use open
instead.
您也可以将其作为 C++fstream
或 C 来打开FILE
,但我不建议这样做。如果您open
改为使用,您将有更好的时间查找示例代码并在论坛上获得帮助。
回答by dubvfan87
you can dump disk sectors to a file with the dd command and read the file generated
您可以使用 dd 命令将磁盘扇区转储到文件中并读取生成的文件
回答by Rob?
As others have correctly pointed out, disk access on Linux (and other Unix-like operating systems) is via a device special file. On my Ubuntu laptop, my hard drive is named "/dev/sda".
正如其他人正确指出的那样,Linux(和其他类 Unix 操作系统)上的磁盘访问是通过设备特殊文件进行的。在我的 Ubuntu 笔记本电脑上,我的硬盘名为“/dev/sda”。
Since you specifically ask how to do it in C++ (not merely how to do it in Linux), here is how to read one sector using std::ifstream
.
由于您特别询问如何在 C++ 中执行此操作(不仅仅是如何在 Linux 中执行此操作),因此这里是如何使用std::ifstream
.
#include <fstream>
#include <cerrno>
#include <stdexcept>
#include <cstring>
#include <vector>
int main() {
// Which disk?
char diskName[] = "/dev/sda";
std::string diskError = std::string() + diskName + ": ";
// Open device file
std::ifstream disk(diskName, std::ios_base::binary);
if(!disk)
throw(std::runtime_error(diskError + std::strerror(errno)));
// Seek to 54321'th sector
disk.seekg(512 * 54321);
if(!disk)
throw(std::runtime_error(diskError + std::strerror(errno)));
// Read in one sector
std::vector<char> buffer(512);
disk.read(&buffer[0], 512);
if(!disk)
throw(std::runtime_error(diskError + std::strerror(errno)));
}