Thread: Hex editor problem

  1. #1
    Registered User
    Join Date
    Nov 2008
    Location
    My computer
    Posts
    65

    Hex editor problem

    I wrote a hex viewer with the following code

    Code:
    #include <stdio.h>
    #include <sys/stat.h>
    #include <sys/types.h>
    #include <time.h>
    
    int main(int argc, char *argv[])
    {
    	struct stat fbuf;
    	FILE *dump;
    	int i;
    	int x = 0;
    	unsigned int size;
    	if(argc<2)
    	{
    		puts("Format: HEXDUMP filename");
    		exit(0);
    	}
    	dump = fopen(argv[1],"rb");
    	if(dump == NULL)
    	{
    		printf("Error opening %s.",argv[1]);
    		exit(0);
    	}
    	size = stat(argv[1],&fbuf);
    	size = fbuf.st_size;
    	while(i=fgetc(dump), size != 0)
    	{
    		printf("%0.2X ",i);
    		x++;
    		size--;
    	    if(!(x%16)) 
    		{
    			putchar('\n');
    		}
    	}
    	fclose(dump);
    	return(0);
    }
    What do I do so I can actually EDIT the info?

  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
    Same as any other editor.

    You read (some) of the file into a buffer.
    You create a visual representation of the file.
    You ask the user what they want to change (maybe they just point and click, or move a cursor).
    When they hit "save" you write the file.
    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
    Nov 2008
    Location
    My computer
    Posts
    65
    I don't understand????!!!!!

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    How about beginning with the simple stuff, like which OS/Compiler you're using, and what kind of user interface (console, GUI), and whether you're going to use some kind of library to manage the screen / input.
    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.

  5. #5
    Registered User
    Join Date
    Nov 2008
    Location
    My computer
    Posts
    65
    I am using Vista and XP. I use gcc for a compiler, and I am using a console interface. SIDE NOTE: can gcc make DOS-compatible 16-bit apps?

  6. #6
    Registered User Maz's Avatar
    Join Date
    Nov 2005
    Location
    Finland
    Posts
    194
    With console you might really want to use some non standard library to handle the screen. One of the simple (and still usable) libraries is ncurses.

    Other than that, it is pretty much what salem said. All editors tend to do those things. 1. Read file (or piece of large file) in memory buffer, give it on user's display, allow editing, and when save is requested, write buffer back to a file.

    EDIT: And if it was not console, then you did want to use non standard library

  7. #7
    Registered User
    Join Date
    Nov 2008
    Location
    My computer
    Posts
    65
    But how do you allow editing?

  8. #8
    Registered User Maz's Avatar
    Join Date
    Nov 2005
    Location
    Finland
    Posts
    194
    Thats up to the editing interface you design. You can for example allow user to hit E (as edit key), or A as add key, enter the location of the new mark, and then give the new mark. It is easy for you to handle, but I doubt your editor becomes popular

    Or then you want to allow user to move the cursor on screen and update contents (here's when ncurses will be handy) as most editors do.

    Or then you can just invent something funny and original, perhaps allow user to enter new chars using on morse codes and mouse button?

    Sorry, I know I am not giving the answer you want to read, but I really cannot give it. I just suggest you to browse some library which allows you to handle cursor on screen more efficiently than standard C libraries, (and as I said, ncurses is one option, though not the only),

  9. #9
    Registered User
    Join Date
    Nov 2008
    Location
    My computer
    Posts
    65
    Let me rephrase that, How can you allow editing using the standard C library?I do not want to use the mouse.

  10. #10
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    But how do you allow editing?
    AFAIK you dont 'edit' files on disk. You load them into RAM, edit them in RAM, then write the content out to disk when you want to save it. Currently you're just reading a character at a time and printing it out to the screen. So, what you need to do is:

    1) Learn how to allocate memory for your file (plus some extra incase you want to add something)
    2) Load the file into memory
    3) Edit the content in memory
    4) Learn how to write the content out to disk

    Try googling 'malloc' and 'fwrite'

  11. #11
    Registered User
    Join Date
    Nov 2008
    Location
    My computer
    Posts
    65
    I know how to use malloc and fwrite. What I want to know is how to put characters on the screen and let users edit them. My problem isn't in the loading or saving, it's the editing.
    Last edited by Sly; 02-15-2009 at 02:37 PM.

  12. #12
    Registered User Maz's Avatar
    Join Date
    Nov 2005
    Location
    Finland
    Posts
    194
    If you want to move the cursor and allow the editing as normal editors do... ...Then I really doubt you want to use only standard C libraries. If you do, and if you manage to do it, I am really interested seeing how you solved the problem

  13. #13
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    Well the first step is, obviously, to make a representation in RAM. Which is something you havent shown yet. I'd recommend you start with that.

  14. #14
    Registered User
    Join Date
    Nov 2008
    Location
    My computer
    Posts
    65
    Libraries are written using standard C keywords. There has to be a way to do it. What about inline asm?

  15. #15
    Registered User
    Join Date
    Nov 2008
    Location
    My computer
    Posts
    65
    So, like... load something from disk, then use malloc to allow space for it... I'm starting to get the picture, but what would I do? Store it in an array? I can make a program that will edit a structure, but a string is harder. Would I represent it as a long string?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Ascii to hex and hex to Ascii
    By beon in forum C Programming
    Replies: 1
    Last Post: 12-26-2006, 06:37 AM
  2. Hex Editing help and information please...
    By SG57 in forum C Programming
    Replies: 9
    Last Post: 06-25-2006, 12:30 AM
  3. Replies: 11
    Last Post: 03-24-2006, 11:26 AM
  4. Replies: 3
    Last Post: 01-23-2006, 07:25 PM
  5. Is binary HEX?
    By Budgiekarl in forum Tech Board
    Replies: 11
    Last Post: 11-23-2003, 09:02 AM