readbin.cpp

Download readbin.zip.

This program shows how to read a binary file produced by the Nios II objcopy utility. This example prints hex address and 32-bit word contents.


#include <stdio.h>

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

	if (argc<3) {
		printf("usage: readbin input_file output_file\n");
		return -1;
	}

	FILE *in, *out;
	errno_t err;
	filename = argv[1];
	err = fopen_s(&in,filename,"rb");
	if (err) {
		printf("file: %s not found\n",filename);
		return -1;
	}
	filename = argv[2];
	err = fopen_s(&out,filename,"wt");
	if (err) {
		printf("file: %s not opened\n",filename);
		return -1;
	}
	int count;
	unsigned int data;
	unsigned int address = 0;
	while (count = fread(&data,4,1,in)) {
		fprintf(out,"%08x : %08x\n",address,data);
		address+=4;
	}
	fclose(in);
	fclose(out);
	return 0;
}


Maintained by John Loomis, updated Sun Feb 04 21:28:29 2007