Read_file_bytes


Yesterday, I needed a tool for reading files as bytes. Hexdump programs generally do some formatting and stuff. I did not want that. I wanted to have a straight forward, simple sweet tool, that simply reads a file and prints it’s bytes. Time required for searching such a tool is indeed greater than the amount of time you can simply write a tool by yourself. I wrote the following:

/* read passed file and show bytes in hex val, consicutive each byte */
/* Released in Public domain by nafSadh.khan */
#include <stdio.h>
#include <string.h>

int main(int argc, char *argv[])
{
	FILE * pFile;
	int c;
	int n = 0;
	if (argc < 2) return 0;
	if (strlen(argv[1]) < 1) return 0;

	pFile = fopen(argv[1], "r");
	if (pFile == NULL) perror("Error opening file");
	else {
		while ((c = fgetc(pFile)) != EOF){
			printf("%02x",c);
			n++;
			if (n % 32 == 0) printf("\n");
		};
		fclose(pFile);
		printf("\n");
	}
	return 0;
}

Next time, I am not re-writing this same program though.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.