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 9138991
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T09:19:45+00:00 2026-06-17T09:19:45+00:00

I am trying to build my own C program that basically works just like

  • 0

I am trying to build my own C program that basically works just like fdisk vdisk ‘p’ command. I just want to be able to read in the first 512 bytes of the disk, lseek to the start of the partitions at (0x1BE) and then read the partition type, name, size, ect. I am unsure how to actually read these values. I have used the read() linux function to read in 512 bytes but when I try displaying/viewing them in any way, nothing is shown. What am I doing wrong?

int main(int argc, char *argv[]) {
    int bytes_read;
    char mbr[512];
    int file;
    if(argc == 1) {
        // Print some help info
        printf ("Here is some help info: \n\n");
    } else if(argc < 3) {
        printf("File: %s\n\n", argv[1]);
        file = open(argv[1], O_RDONLY);
        lseek(bytes_read, 0, 0);
        //First get the MBR
        bytes_read = read(file, mbr, 512);
        printf("MBR=%s\n\nbytes_read=%d\n\n", mbr, bytes_read);
    } else {
        printf ("Incorrect usage: fdisk <disk>\n\n");
    }
}
  • 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-17T09:19:46+00:00Added an answer on June 17, 2026 at 9:19 am

    Don’t try to use printf with binary data. If your binary data starts with a NUL (ASCII 0), then printf will assume you’ve got an empty string. You can use write() to write out arbitrary data (it takes a buffer and length), e.g:

    #include <unistd.h>
    
    write(STDOUT_FILENO, mbr, 512)
    

    …but even this won’t necessarily display anything useful, because your terminal may try to interpret control characters in the output. You’re best bet would then be to pipe the output to something like xxd or od, both of which will produce a hexdump of their input data.

    For example, the first 512 bytes of my local drive are all NUL. Using write() in your code (and removing that lseek) results in 512 NUL bytes on output. Try passing something other than disk to your code, e.g.:

    myexe /etc/passwd
    

    The structure of a standard DOS MBR is documented here, suggesting that you might start with data structures like this:

    struct _partition {
            uint8_t         status;
            uint8_t         chs_start[3];
            uint8_t         part_type;
            uint8_t         chs_end[3];
            uint32_t        lba_start;
            uint32_t        sectors;
    };
    

    And populate it something like this:

            fd = open(target, O_RDONLY);
            lseek(fd, 446, SEEK_SET);
            for (i=0; i<4; i++) {
                    struct _partition p;
    
                    bytes_read = read(fd, &p, sizeof(struct _partition));
    
                    // assume this prints information to stdout or something.
                    print_part(i, &p);
            }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to build my own boot loader that loads then switches from
I am trying to build my own simple template handler to easily be able
I 'm trying to build my own simple breadcrumb component that pushes a PageRefence
I am trying to build my own little toolbox for Vista. One of the
I'm trying to build my own template tags. I have no idea why I
I'm trying to build my own custom control for a windows forms application in
I'm trying to build my own MVC as a practice and learning experience. So
I'm trying to build a model viewer in opengl, but got stumped... I basically
I am trying to write some program using the OpenKinect project. I want to
I am trying to write a program that will be installed on a Linux

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.