在Linux上读取C ++中的硬盘扇区 [英] Reading Hard Disk Sectors in C++ on Linux
问题描述
如何使用gcc / linux读取C ++中的硬盘扇区?是否有一个标准库,我可以使用或必须下载一些东西?在Windows中,我可以使用CreateFile(...)访问原始磁盘扇区,但我不知道如何在Linux中。
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.
我在Ubuntu LTS 10.4上使用GCC。感谢您的帮助。
I am using GCC on Ubuntu LTS 10.4. Thank you for your help.
推荐答案
硬盘只是另一个文件(不是常规文件 ,但仍然,一个文件)。打开它的正常方式...
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 ...
权限。注意,/ dev / sda1
只是一个例子,它是磁盘上的第一个分区 sda
路径将取决于您的系统。您可以使用 mount
命令列出安装点,您可以使用 / dev / sda $ c>访问整个磁盘(而不仅仅是分区) $ c>,
/ dev / sdb
等。
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.
您也可以打开它作为C ++ fstream
或C FILE
,但我不推荐这样做。如果您使用 open
,您将有更好的时间查找示例代码并在论坛上获得帮助。
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.
这篇关于在Linux上读取C ++中的硬盘扇区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!