C语言中的低级写作 [英] Low-Level-Writing in C

查看:151
本文介绍了C语言中的低级写作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用C编程语言写入硬盘上的任何块?

How can I write to any block on my HDD using the C programming language?

关于撰写 MBR 的问题但它并没有涵盖C方面那么多。

There was a question about writing the MBR but it didn't cover the C aspects that much.

由于文件描述符 - 如文所说 - 对于文件,我想在这里没有办法使用它们案件。包括低级I / O 在C标准库中也使用filedescriptors实现。

Since filedescriptors are - as the word says - for files, I guess there is no way to use them in this case. The Low-level I/O included in the C standard library is also implemented with filedescriptors.

更精确地说:

这个问题是相当的关于写入硬盘块而不是文件(独立于操作系统)。

To put it more precisely:
This question is rather about writing HDD blocks than files (OS independent).

上述问题的答案基本上建议使用 dd (coreutils)。这就是为什么我要在C中寻求一种方式。也许是bootloaders( GRUB )并启动扇区病毒使用不同的技术?

The answers to the question mentioned above basically suggested using dd (coreutils) on UNIX-Systems. This is why I am asking for a way in C. Maybe bootloaders (GRUB) and boot sector viruses use different techniques?

我想改变文件描述符内部的实际指针是不合法的。

I guess changing the actual pointer inside of a filedescriptor is not a legitimate way.

问题和限制:

我知道有些方面需要牢记,例如

Problems and limitations:
I know that there are certain aspects to keep in mind, such as


  • 某些操作系统限制对卷的直接访问(例如Windows)

  • 编写错误以及将错误的数据写入某些块
    可能会导致文件系统损坏(丢失硬盘上的数据)。

  • 防病毒软件可能会将其标记为可疑代码。

这个问题更具理论性。

推荐答案

C语言可以访问功能的文件fopen / fclose / fread / fwrite 等但是没有这样的东西语言中的阻止设备(就此而言,甚至不是设备)。

The C language has access to files with functions fopen/fclose/fread/fwrite etc. But there is no such thing as a block device in the language (not even a device, for that matter).

POSIX另一方面具有低级函数打开/关闭/读/写来访问文件,并具有块设备的概念。只要您遵循一些简单的规则(主要是块对齐)并且您知道设备的名称(可以可以(小心)用于块设备> / dev / xxx )。

POSIX on the other hand has the low level functions open/close/read/write to access to files, and have the concept of block device. These functions can be used (with care) for a block device, as long as you follow a few simple rules (block alignment, mainly) and you know the name of your device (/dev/xxx).

如果您在非POSIX系统(如Windows)中,则操作系统将具有特定的处理块设备访问的方法。例如,在Windows中,您可以使用 CreateFile 函数,设备名称为 \\.\PhysicalDrive0 \\.\C:等。

If you are in a non-POSIX system, such as Windows, then the OS will have a specific way to handle the block device access. In Windows, for example, you can use the CreateFile function with the device name \\.\PhysicalDrive0, \\.\C: or such.

这篇关于C语言中的低级写作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆