微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

打印分区表 – C程序

我试图打印使用C编程语言的分区表,一切似乎工作正常:打开和阅读,但我不明白为什么它打印垃圾值。

这里是代码

struct partition { unsigned char drive; unsigned char chs_begin[3]; unsigned char sys_type; unsigned char chs_end[3]; unsigned char start_sector[4]; unsigned char nr_sector[4]; }; int main() { int gc = 0,i = 1,nr = 0,pos = -1,nw = 0; int fd =0; char buf[512] ; struct partition *sp; printf("Ok "); if ( (fd = open("/dev/sda",O_RDONLY | O_SYNC )) == -1) { perror("Open"); exit(1); } printf("fd is %d n",fd); pos = lseek (fd,SEEK_CUR); printf("Position of pointer is :%dn",pos); if ((nr = read(fd,buf,sizeof(buf))) == -1) { perror("Read"); exit(1); } close(fd); printf("Size of buf = %dn and number of bytes read are %d ",sizeof(buf),nr); if ((nw = write(1,64)) == -1) { printf("Write: Error"); exit(1); } printf("nn %d bytes are just been written on stdoutn",nw,"this can also be printedn"); printf("ntt*************Partition Table****************nn"); for (i=0 ; i<4 ; i++) { sp = (struct partition *)(buf + 446 + (16 * i)); putchar(sp -> drive); } return 0; }

它是打印垃圾而不是分区表。

我可能会有一些基本的理解问题,但是我用Googlesearch了很长时间,但是这并没有真正的帮助。 我也看到了fdisk的源代码,但是在这一点上我是无法理解的。 任何人都可以请指导我? 我不希望有人清除我的错误,并给我工作代码。 只需一两句话 – 或任何链接

如何在CentOs中启动后编辑initramfs来添加新的分区

主分区的最大数量是多less?

跨平台的分区pipe理库?

如何使你的电脑的Android分区的形象

将Postgresql数据库从Windows移到不同的sorting规则

如何初始化文件系统?

如何在Windows上使用C ++将包含多个分区的映像写入USB闪存驱动器

如何使分手始终显示相同的单位

在linux上用c ++移动文件的更快的方法

diskpart脚本删除所有分区

尝试这个:

#include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> struct partition { unsigned char boot_flag; /* 0 = Not active,0x80 = Active */ unsigned char chs_begin[3]; unsigned char sys_type; /* For example : 82 --> Linux swap,83 --> Linux native partition,... */ unsigned char chs_end[3]; unsigned char start_sector[4]; unsigned char nr_sector[4]; }; void string_in_hex(void *in_string,int in_string_size); void dump_partition(struct partition *part,int partition_number); void dump_partition(struct partition *part,int partition_number) { printf("Partition /dev/sda%dn",partition_number + 1); printf("boot_flag = %02Xn",part->boot_flag); printf("chs_begin = "); string_in_hex(part->chs_begin,3); printf("sys_type = %02Xn",part->sys_type); printf("chs_end = "); string_in_hex(part->chs_end,3); printf("start_sector = "); string_in_hex(part->start_sector,4); printf("nr_sector = "); string_in_hex(part->nr_sector,4); } void string_in_hex(void *in_string,int in_string_size) { int i; int k = 0; for (i = 0; i < in_string_size; i++) { printf("%02x ",((char *)in_string)[i]& 0xFF); k = k + 1; if (k == 16) { printf("n"); k = 0; } } printf("n"); } int main(int argc,char **argv) { int /*gc = 0,*/ i = 1,pos = -1/*,nw = 0*/; int fd = 0; char buf[512] ; struct partition *sp; int ret = 0; printf("Ok "); if ((fd = open("/dev/sda",O_RDONLY | O_SYNC)) == -1) { perror("Open"); exit(1); } printf("fd is %dn",sizeof(buf))) == -1) { perror("Read"); exit(1); } ret = close(fd); if (ret == -1) { perror("close"); exit(1); } /* Dump the MBR buffer,you can compare it on your system with the output of the command: * hexdump -n 512 -C /dev/sda */ string_in_hex(buf,512); printf("Size of buf = %d - and number of bytes read are %dn",nr); /*if ((nw = write(1,64)) == -1) { printf("Write: Error"); exit(1); } printf("nn%d bytes are just been written on stdoutnthis can also be printedn",nw); */ //printf("ntt*************Partition Table****************nn"); printf("ntt*************THE 4 MAIN PARTITIONS****************nn"); /* Dump main partitions (4 partitions) */ /* Note : the 4 partitions you are trying to dump are not necessarily existing! */ for (i = 0 ; i < 4 ; i++) { sp = (struct partition *)(buf + 446 + (16 * i)); //putchar(sp->boot_flag); dump_partition(sp,i); } return 0; }

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐