生活随笔
收集整理的這篇文章主要介紹了
直接读取硬盘扇区
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
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; ????} ????
|
轉載于:https://www.cnblogs.com/yunnotes/archive/2013/04/19/3032446.html
總結
以上是生活随笔為你收集整理的直接读取硬盘扇区的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。