直接读取硬盘扇区
2013-04-19 14:07
253 查看
Linux系统下一切都是文件,可以像使用普通文件一样使用设备,可直接操作设备扇区内容,这种方式不经过文件系统。
#include <stdio.h> #include <stdlib.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <libgen.h> #include <unistd.h> static void usage(char *prog_name) { fprintf(stderr, "usage: %s device start count\n", prog_name); fprintf(stderr, "example: %s /dev/sda 0 512\n", prog_name); } int main(int argc, char *argv[]) { char buf[4096]; int fd = 0; int i = 0; int j = 0; int start = 0; int count = 0; char *device = NULL; if(argc != 4){ usage(basename(argv[0])); exit(1); } device = argv[1]; start = atol(argv[2]); count = atol(argv[3]); fd = open(device, O_RDONLY); if(-1 == fd){ fprintf(stderr, "cannot open /dev/hda"); exit(1); } if(lseek(fd, start, SEEK_SET) != start){ fprintf(stderr, "cannot seek at %d", start); exit(1); } while(count > 0){ int size = count > sizeof(buf) ? sizeof(buf) : count; read(fd, buf, size); /* 每行显示16个字节 每两个字节间以空格分开 */ for(i = 0; i < size/16; i++){ fprintf(stdout,"%08x:", i*16+start); for(j = 0; j < 16; j++){ fprintf(stdout, " %02x", (int)buf[i*16+j] & 0xFF); } fprintf(stdout,"\n"); fflush(stdout); } count -= size; start += size; } |
相关文章推荐
- windows sdk编程系列文章 --- 直接从硬盘扇区读取文件内容
- WIN32和Kernel直接读写硬盘扇区
- 打开被独占的文件方法(三) -- 使用直接硬盘访问读取文件
- babyos (十) —— 通过IO端口读取硬盘扇区
- (WIN32和Kernel)直接读写硬盘扇区.
- linux2.6硬盘扇区直接读写程序
- linux2.6硬盘扇区直接读写程序 分类: 磁盘的扇区读写 2015-04-29 10:37 317人阅读 评论(0) 收藏
- linux读取硬盘指定扇区
- linux2.6硬盘扇区直接读写程序
- (WIN32和Kernel)直接读写硬盘扇区
- WIN32和Kernel)直接读写硬盘扇区
- linux2.6硬盘扇区直接读写程序
- linux2.6硬盘扇区直接读写程序
- 读取硬盘的第一扇区数据(MBR+DPT+Signature)
- NTRing3下使用CreateFile,ReadFile直接读取硬盘实例
- 读取硬盘扇区的数据
- 直接写入硬盘的第一扇区
- 通过文件的方式对硬盘扇区进行直接读写操作
- 读取硬盘的MBR引导扇区(Windows各系统通用)
- WIN32和Kernel直接读写硬盘扇区.