Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
打印分区表-C程序_C_Linux_Filesystems_Operating System_Partitioning - Fatal编程技术网

打印分区表-C程序

打印分区表-C程序,c,linux,filesystems,operating-system,partitioning,C,Linux,Filesystems,Operating System,Partitioning,我正在尝试使用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

我正在尝试使用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, 0, SEEK_CUR);

    printf("Position of pointer is :%d\n", pos);
    if ((nr = read(fd, buf, sizeof(buf))) == -1)
    {
        perror("Read");
        exit(1);
    }

    close(fd);

    printf("Size of buf = %d\n and number of bytes read are %d ", sizeof(buf), nr);


    if ((nw = write(1, buf, 64)) == -1)
    {
        printf("Write: Error");
        exit(1);
    }

    printf("\n\n %d bytes are just been written on stdout\n", nw,"this can also be printed\n");

    printf("\n\t\t*************Partition Table****************\n\n");

    for (i=0 ; i<4 ; i++)
    {
        sp = (struct partition *)(buf + 446 + (16 * i));
        putchar(sp -> drive);
    }


    return 0;
}
struct分区
{
无符号字符驱动器;
无符号字符chs_begin[3];
无符号字符系统类型;
无符号字符chs_end[3];
无符号字符起始扇区[4];
无符号字符nr_扇区[4];
};
int main()
{
int gc=0,i=1,nr=0,pos=-1,nw=0;
int-fd=0;
char-buf[512];
结构分区*sp;
printf(“Ok”);
如果((fd=open(“/dev/sda”,O|RDONLY | O|u SYNC))=-1)
{
佩罗(“公开”);
出口(1);
}
printf(“fd是%d\n”,fd);
pos=lseek(fd,0,SEEK_CUR);
printf(“指针位置为:%d\n”,位置);
如果((nr=读取(fd,buf,sizeof(buf)))=-1)
{
佩罗(“阅读”);
出口(1);
}
关闭(fd);
printf(“buf的大小=%d\n和读取的字节数为%d”,sizeof(buf),nr);
if((nw=write(1,buf,64))=-1)
{
printf(“写入:错误”);
出口(1);
}
printf(“\n\n%d个字节刚刚被写入stdout\n”,nw,“这也可以被打印”);
printf(“\n\t\t***************************************************\n\n”);
对于(i=0;i驱动);
}
返回0;
}
它正在打印垃圾,而不是分区表

我可能有一些基本的理解问题,但我用谷歌搜索了很长时间,但它并没有真正的帮助。我也看到了fdisk的源代码,但这超出了我的理解范围。谁能给我指路吗?我不希望有人来纠正我的错误,给我工作代码。只是一两句话——或者任何链接

试试这个:

#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%d\n", partition_number + 1);
        printf("boot_flag = %02X\n", part->boot_flag);
        printf("chs_begin = ");
        string_in_hex(part->chs_begin, 3);
        printf("sys_type = %02X\n", 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, nr = 0, 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 %d\n", fd);

        pos = lseek (fd, 0, SEEK_CUR);

        printf("Position of pointer is :%d\n", pos);
        if ((nr = read(fd, buf, 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 %d\n", sizeof(buf), nr);
        /*if ((nw = write(1, buf, 64)) == -1)
        {
                printf("Write: Error");
                exit(1);
        }

        printf("\n\n%d bytes are just been written on stdout\nthis can also be printed\n", nw);
        */
        //printf("\n\t\t*************Partition Table****************\n\n");
        printf("\n\t\t*************THE 4 MAIN PARTITIONS****************\n\n");

        /* 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;
}
#包括
#包括
#包括
#包括
#包括
#包括
结构分区
{
unsigned char boot_flag;/*0=未激活,0x80=激活*/
无符号字符chs_begin[3];
未签名字符sys_type;/*例如:82-->Linux交换,83-->Linux本机分区*/
无符号字符chs_end[3];
无符号字符起始扇区[4];
无符号字符nr_扇区[4];
};
以十六进制表示的无效字符串(以十六进制表示的无效*字符串,以十六进制表示的整数);
无效转储分区(结构分区*部分,整数分区编号);
无效转储分区(结构分区*部分,整数分区编号)
{
printf(“分区/dev/sda%d\n”,分区号+1);
printf(“启动标志=%02X\n”,部件->启动标志);
printf(“chs_begin=”);
十六进制字符串(部分->chs\U开始,3);
printf(“系统类型=%02X\n”,部件->系统类型);
printf(“chs_end=”);
十六进制字符串(部分->chs\U端,3);
printf(“开始扇区=”);
十六进制字符串(部分->起始扇区,4);
printf(“nr_sector=”);
以十六进制表示的字符串(部分->nr_扇区,4);
}
以十六进制表示的无效字符串(以十六进制表示的无效*字符串,以十六进制表示的整数)
{
int i;
int k=0;
对于(i=0;i/*注意:您尝试转储的4个分区不一定存在*/
对于(i=0;i<4;i++)
{
sp=(结构分区*)(buf+446+(16*i));
//putchar(sp->boot_标志);
转储分区(sp,i);
}
返回0;
}

<代码>您是否考虑过“代码> fDIS</代码>或其他分区实用程序的源代码?@ BaselestalyKyvigsiStaynkvigy:是的,他根据他的帖子做了。”装饰你的问题发生在这一行吗<代码>printf(“\n\t\t******************************************************\n\n”)?或者你指的是之后的输出?什么是
驱动器
变量类型?我认为
分区条目
的第一个字节是引导指示符(bootable)标志。它可能具有
0
0x80
值。看起来您正试图将其用作驱动器号。您的
write()
调用将打印存储在MBR中的启动代码。如果要打印分区表,应该从
buf+446
开始打印。然后,正如@fasked所说,分区条目不包含驱动器号(无论如何,它只在像OS这样的DOS中使用),而是一个活动分区指示符。非常感谢你们的帮助。”注意:4个分区