Hello, I am in a C+ programming class at my college where I am at, and for one fo my labs I have to create a program similar to hexdump of linux, I can do everything it asks me to do, but I can't figure out how to do the input buffer, can somebody send me a code that would make the program read a file 16 bytes at a time. And oculd you please send it to edited by moderator Thanks in advance.

Dani AI

Generated

Thread goal: produce a hexdump-style read that processes a file in 16‑byte chunks. correctly discouraged sending mail; and suggested line- and char-oriented reads. For a binary dump those approaches work but are clumsier than block reads. A single block read returns the exact number of bytes available (0–16) and handles the final partial line cleanly.

Compact C example (reads 16 bytes, prints offset, hex bytes and printable ASCII):

#include <stdio.h>
#include <ctype.h>

int main(int argc, char **argv)
{
    FILE *f = fopen(argc>1 ? argv[1] : "/dev/stdin", "rb");
    if (!f) return 1;

    unsigned char chunk[16];
    size_t off = 0, n;

    while ((n = fread(chunk, 1, sizeof chunk, f)) > 0) {
        printf("%08zx  ", off);
        for (size_t i = 0; i < sizeof chunk; ++i) {
            if (i < n) printf("%02x ", chunk[i]);
            else        printf("   ");
            if (i == 7) putchar(' ');
        }
        putchar(' ');
        for (size_t i = 0; i < n; ++i)
            putchar(isprint(chunk[i]) ? chunk[i] : '.');
        putchar('\n');
        off += n;
    }
    fclose(f);
    return 0;
}

C++ variant: open with std::ifstream in(path, std::ios::binary) and use in.read(reinterpret_cast<char*>(buf), sizeof buf) then in.gcount() to discover how many bytes were produced on the last call; process exactly that many bytes.

Practical notes: open files in binary mode ("rb" or ios::binary), treat the buffer as raw bytes (no NUL-termination), always use the read return value (fread size or gcount) to detect a short final block, and cast to unsigned char when formatting %02x so sign-extension doesn't corrupt output. Padding the hex area for the last, short line keeps columns aligned.

Recommended Answers

All 9 Replies

We don't write your code for you-- we're here to help you learn.

Please at least attempt to write something, and meanwhile, we might be able to give you some pointers. Also, posting your email address for us to send you the output is also counterproductive to the goal of this forum; we want to make our knowledge available to all, not just you.

That is the thing I can't figure out how to do the buffer, that is why I am asking somebody to show me how to do it.

commented: Show your attempt first. And use code tags. +0

Oh here is the code I have so far, how can I fix it to where it reads 16 bytes at a time.

>how can I fix it to where it reads 16 bytes at a time.
Without looking at the code:

char buffer[17];

...

fgets ( buffer, sizeof buffer, in );

How exactly is that suppose to fix anything?

Your code tries to read 16 chars from the console, but you say you need to be reading from a file. As Naru points out, you can use fgets, or you can use fgetc() or something similar.

int count = 0;
int c;
while ((count < 16 && ((c = fgetc(f)) != EOF))
{
// increment count, save c in an array
}
// here count may be 16, or it may be less if you hit EOF.

>How exactly is that suppose to fix anything?
It reads 16 bytes at a time using a buffer, moron. That's what you asked for. I would give a more detailed answer, but I refuse to download attachments like almost every other experienced board member. If you want detailed help then give a detailed problem. And I'll add that the tone of your posts is annoying. Consider your next post carefully if you want any real help from me, as I can be easily insulted.

ok, sorry I didn't mean to insult, I didn't think that was insulting, but anyway what would be a code that works for an 16 byte buffer.

>but anyway what would be a code that works for an 16 byte buffer.
Be more specific. The following is a 16 byte buffer:

char buffer[16];

My code added one to the size for a trailing null character added by fgets. Clearly you aren't communicating your problem adequately or my suggestion would have been a reasonable solution.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.