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

  • Home
  • SEARCH
  • 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 6113791
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T14:53:58+00:00 2026-05-23T14:53:58+00:00

There I have this assignment where I am suppose to emulate the hexdump function

  • 0

There I have this assignment where I am suppose to emulate the hexdump function from unix. I got my program to dump files into hex but i am having trouble printing the offset and the printable characters.

Here is my code:

/*performs a hex and an octal dump of a file*/

#include <stdio.h>
#include <string.h>
#include <ctype.h>
#define LINESIZE 512

void hexdump(FILE *fp);
void octaldump(FILE *fp);

int main(int argc, char * argv[])
{
    FILE *fp;

   if((fp = fopen(argv[1], "rb+")) == '\0')
   {
    perror("fopen");
    return 0;
   }

   hexdump(fp);

   return 0;
}

void hexdump(FILE *fp)
{
   char temp [LINESIZE];
   size_t i = 0;
   size_t linecount = 1;
   long int address = 0;


   while(fscanf(fp," %[^\n] ", temp) == 1)
   {
     printf("%5d", address);

     for(i = 0; i < strlen(temp); i++)
     {
        printf("%02x ", temp[i]);

        if(linecount == 16)
        {
            printf("\n");
            linecount = 0;          
        }
        linecount++;
     }
     printf(" | ");

     for(i = 0; i < strlen(temp); i++)
     {
         if(temp[i] < 32)
         {
            printf(".");
         }
         else
         {
             printf("%c", temp[i]);
         }
     }
     printf("\n");
    }
}

As i said above, my program is suppose to print the offset then the hex value of the file padded with 0’s, then the printable characters of the file like so.

0000000 7f 2a 34 f3 21 00 00 00 00 00 00 00 00 00 00 00 | test file

I managed to print out the printable characters, but they appear after the hex part. So if one line of hexcode extends to the next line, it prints the printable characters on the nextline.

For example:

2a 3b 4d 3e 5f
12 43 23 43 | asfdg df

How do i get it to print whatever character appears after one line of the hex characters?

PS: For some reason my program doesn’t pad 0’s for some reason.

EDIT1: I got the offset part, i just keep adding 16 to my address variable and keep printing

  • 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-05-23T14:53:59+00:00Added an answer on May 23, 2026 at 2:53 pm

    Since this is homework, I’ll just give you some pointers on how to approach your problem. I’ll be as vague as I can so as to not interfere with your learning.

    First of all, don’t use fscanf if you’re writing a hex dumper, use fread instead. The scanf family of functions are meant for text and hex dumpers are usually used with binary data.

    If you use fread and read just enough bytes at a time to fill one line of output, then you can produce your desired output with two for loops (one for hex, one for characters) followed by a single newline.

    As far as your zero padding problem goes, read the printf man page, your "%5d" format string needs a little bit more.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

There have been a couple of questions that sort of dealt with this but
I have this legacy code that I am working with and there is code
i have this block of xslt if-else case and was wondering if there's a
I know there have been a few threads on this before, but I have
I have this unordered list <ul> <li>two</li> <li>three</li> </ul> Is there a way I
I have seen this question: Are there any decent UI components for touch screen
This is a little out there but I have a customer object coming back
Is there anyway to have the var be of a nullable type? This implicitly
I was just wondering whether there is some way to do this: I have
There is probably is simple fix for this but I currently have code similar

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.