Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8468397
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T15:55:11+00:00 2026-06-10T15:55:11+00:00

I am trying to print a partition table using C programming language, everything seems

  • 0

I am trying to print a partition table using C programming language, everything seems to work fine: Opening and reading, but I don’t understand why it is printing garbage values.

Here is the code:

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;
}

It is printing garbage instead of partition table.

I might have some basic understanding issues but I searched with Google for a long time but it did not really help. I also saw the source code of fdisk but it is beyond my understanding at this point. Could anyone please guide me? I am not expecting someone to clear my mistake and give me the working code. Just a sentence or two – or any link.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T15:55:12+00:00Added an answer on June 10, 2026 at 3:55 pm

    Try this:

    #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;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Im trying to print, using a xslt sheet a url but im having problems
I'm trying to print an external site's HTML using curl, but when I go
I am trying to print a particular text using awk if string is empty.
Trying to print out some html forms but I get a parsing syntax error.
I'm trying to print out the body of a HTTP response using Python. Here
I'm trying to print the contents of an array to the screen, but nicely
I am trying print a vaules retrived from select but, I could not find
am trying to print these list of numbers below values in my page but
I'm trying to print a datagridview by drawing to bitmap using visual studio 2008
I'm trying to print an object's vtable using gdb; I found the show print

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.