Thread: Display a 8 digit hex offset 0x?

  1. #1
    Registered User
    Join Date
    Feb 2016
    Posts
    12

    Display a 8 digit hex offset 0x?

    Hello I am making a program that is still isn't 100% but I am trying to make this step by step and debug. I am running into a problem. First let me explain what my program is supposed to do.

    It is first supposed to detect terminal size, if terminal size isn't at least 80x20 program will close. Program will detect three parameters if three aren't given it will close.

    The program is supposed to open an entered file and display (so far) a 8 digit hex offset starting with "0x" for several lines, but I can only get it to display one. Would I have to create a 'for' loop to do this?

    I am trying to display all the hex values under "offset"

    Any tips or suggestion would be appreciated!



    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<sys/ioctl.h>
    
    
    int main(int argc,char** argv)
    {
        struct winsize terminal;
        FILE* fp;
        int a, c, column, row, lines;
    
        ioctl(0, TIOCGWINSZ,&terminal, argv[2]);
    
        fp = fopen(argv[1],"r");//set open for argv[1] and set it to read mode
        column = terminal.ws_col;
        row = terminal.ws_row;
        lines = atoi(argv[2]);
    
        // printf("Rows are: %d\n", row);
        // printf("Columns are: %d\n", column);
    
        if(argc <3)//Argument checking, at least three to run
        {
            printf("You need atleast 3 arguments to run!\n");
            exit(1);
        }
    
        if(fp == NULL)
        {
            fprintf(stderr,"Error opening '%s'.\n", argv[1]);//if not file is specified then close program and give an error
            exit(1);
        }
        c = fgetc(fp);//set c to fgetc
    
        if(column <80|| row <20)//if temrinal window is less than 80x20, display an error and exit
        {
            printf("Terminal window must be at least 80x20!\n");//display error and close program if column criteria isn't met
            exit(1);
        }
    
        /*
        while(!feof(fp))                                   //if file is entered, display a test message (this is where later on I will show hex value)
    
        {
            printf("Good Job! You picked a file to manipulate!!\n");
            exit(1);
         }
         */
        if(lines ==0)//if 0 is entered for argv[2], display all the lines, 512 byte file
        {
            printf("------------------------------------------------------------------------------\n");//**this is where I try to display character/hex banner**
            printf("offset      0  1  2  3   4  5  6  7   8  9  a  b   c  d  e  f  ascii\n");
            printf("-------------------------------------------------------------------------------\n");
            for(c ==0x200);//**This is where I try to get the 8 digit 0x hex offset**
            {
                fprintf(stdout,"%04x", c);
            }
        }
        elseif(lines ==20)
        {
            printf("This will print out 20 lines\n");
        }
        elseif(lines ==30)
        {
            printf("This will print out 30 lines\n");
        }
        else
        {
            printf("Please Enter a Valid Number!\n");
        }
    
        fclose(fp);//close fp
    
        return0;
    }
    



  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > if(argc <3)
    You've used argv[] twice before checking whether they exist.

    > for(c ==0x200);//**This is where I try to get the 8 digit 0x hex offset**
    Well you need to read up on the fact that the basic syntax is
    for ( initial ; comparison ; step )

    Also, the ; at the end means you wrote
    Code:
    for ( )
    {
      // do nothing
    }
    {
        fprintf(stdout,"%04x", c);
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Feb 2016
    Posts
    12
    Quote Originally Posted by Salem View Post
    > if(argc <3)
    You've used argv[] twice before checking whether they exist.

    > for(c ==0x200);//**This is where I try to get the 8 digit 0x hex offset**
    Well you need to read up on the fact that the basic syntax is
    for ( initial ; comparison ; step )

    Also, the ; at the end means you wrote
    Code:
    for ( )
    {
      // do nothing
    }
    {
        fprintf(stdout,"%04x", c);
    }
    Thank you. I will fix!

  4. #4
    Registered User
    Join Date
    Feb 2016
    Posts
    12
    Still having some issues trying to create a way to display the hex values for multiple lines on my offset column

  5. #5
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    How about posting your current code, a sample of your input file, a sample of the output you are getting with that input file, and lastly a sample of the output you desire with that input file.


    Jim

  6. #6
    Registered User
    Join Date
    Feb 2016
    Posts
    12
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<sys/ioctl.h>
    
    
    
    
    int main(int argc,char** argv)
    {
        struct winsize terminal;
        FILE* fp;
        int a, c, column, row, lines;
    
    
        ioctl(0, TIOCGWINSZ,&terminal, argv[2]);
    
    
        fp = fopen(argv[1],"r");//set open for argv[1] and set it to read mode
        column = terminal.ws_col;
        row = terminal.ws_row;
        lines = atoi(argv[2]);
    
    
        // printf("Rows are: %d\n", row);
        // printf("Columns are: %d\n", column);
    
    
        if(argc <3)//Argument checking, at least three to run
        {
            printf("You need atleast 3 arguments to run!\n");
            exit(1);
        }
    
    
        if(fp == NULL)
        {
            fprintf(stderr,"Error opening '%s'.\n", argv[1]);//if not file is specified then close program and give an error
            exit(1);
        }
        c = fgetc(fp);//set c to fgetc
    
    
        if(column <80|| row <20)//if temrinal window is less than 80x20, display an error and exit
        {
            printf("Terminal window must be at least 80x20!\n");//display error and close program if column criteria isn't met
            exit(1);
        }
    
    
        /*
        while(!feof(fp))                                   //if file is entered, display a test message (this is where later on I will show hex value)
    
    
        {
            printf("Good Job! You picked a file to manipulate!!\n");
            exit(1);
         }
         */
        if(lines ==0)//if 0 is entered for argv[2], display all the lines, 512 byte file
        {
            printf("------------------------------------------------------------------------------\n");//**this is where I try to display character/hex banner**
            printf("offset      0  1  2  3   4  5  6  7   8  9  a  b   c  d  e  f  ascii\n");
            printf("-------------------------------------------------------------------------------\n");
            printf("0x%08x\n" , c);
    
        }
        elseif(lines ==20)
        {
            printf("This will print out 20 lines\n");
        }
        elseif(lines ==30)
        {
            printf("This will print out 30 lines\n");
        }
        else
        {
            printf("Please Enter a Valid Number!\n");
        }
    
    
        fclose(fp);//close fp
    
    
        return0;
    }
    This is my current code with an output of

    Code:
    
    ~//cbf0$ ./cbf0 win7.mbr 0
    ------------------------------------------------------------------------------
    offset      0  1  2  3   4  5  6  7   8  9  a  b   c  d  e  f  ascii
    -------------------------------------------------------------------------------
    0x00000033
    and my desired output would be:

    Code:
    l~/cbf0$ ./cbf0 win7.mbr 0
    ------------------------------------------------------------------------------
    offset      0  1  2  3   4  5  6  7   8  9  a  b   c  d  e  f  ascii
    -------------------------------------------------------------------------------
    0x00000000
    0x00000010
    0x00000020
    0x00000030
    0x00000040
    0x00000050
    0x00000060
    0x00000070
    0x00000080
    0x00000090
    0x000000a0
    "and so on.."
    Last edited by Stan Nichols; 04-03-2016 at 11:59 AM.

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Well you need to put fgetc() inside a loop which does the following
    - reads a character
    - formats it as 2 hex digits
    - generates an ascii representation (if the char is printable).
    - prints an address after reading 16 characters

    Plus the other issues I mentioned 2 days ago.


    Start with something simple like
    Code:
    int addr = 0, numchars = 0;
    while ( (ch=fgetc(fp)) != EOF ) {
      unsigned char c = (unsigned char)ch;
      if ( (numchars++ % 16 ) == 0 ) {
        printf("\n%08x: ", addr);
        addr += 16;
      }
      printf("%02x ", c );
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to display single digit int as four digit C++ Programming
    By Sloganathan in forum C++ Programming
    Replies: 1
    Last Post: 03-06-2012, 11:30 AM
  2. Read from file - 1-digit and 2-digit numbers
    By Bonaventura in forum C Programming
    Replies: 8
    Last Post: 03-06-2010, 06:33 AM
  3. Adding a Large number digit by digit
    By mejv3 in forum C Programming
    Replies: 23
    Last Post: 09-21-2007, 03:00 PM
  4. Adding a Large number digit by digit
    By mejv3 in forum C Programming
    Replies: 1
    Last Post: 09-14-2007, 03:28 AM
  5. Displaying a "7 digit display"
    By Isometric in forum Windows Programming
    Replies: 3
    Last Post: 11-12-2003, 05:06 PM

Tags for this Thread