/*
* binread.c
* ENCM 369 Winter 2006 Lab 11 Exercise A
*/
#include <stdio.h>
#include <stdlib.h>
#define MAX_ELEMENTS 1024
unsigned int input_data[MAX_ELEMENTS];
unsigned int input_temp[MAX_ELEMENTS];
int main(int argc, char **argv)
{
unsigned int size_from_file;
unsigned int how_many_read;
FILE *fp;
unsigned int i;
unsigned int j;
unsigned int temp = 0;
unsigned int b = 0;
if (argc != 2) {
fprintf(stderr, "syntax for command should be: %s filename\n", argv[0]);
exit(1);
}
/* The mode "rb" means read a binary file. The "b" is
* not necessary in Linux systems, but is needed in
* MS Windows and various other OSes. */
if ((fp = fopen(argv[1], "rb")) == NULL) {
fprintf(stderr, "failed to open file %s for input\n", argv[1]);
exit(1);
}
printf("\nAttempting to read array stored in %s ...\n", argv[1]);
how_many_read = fread(&size_from_file, sizeof(unsigned int), 1, fp);
/*Conversion to correct endian-ness.*/
printf("Size_from_file = %u\n", size_from_file);
b = size_from_file & 0x000000ff;
b = b << 24;
temp = temp | b;
b = size_from_file & 0x0000ff00;
b = b << 8;
temp = temp | b;
b = size_from_file & 0x00ff0000;
b = b >> 8;
temp = temp | b;
b = size_from_file & 0xff000000;
b = b >> 24;
temp = temp | b;
size_from_file = temp;
printf("Size_from_file is now = %u\n", size_from_file);
if (how_many_read != 1) {
fprintf(stderr,
"Not enough bytes in %s to form an element count.\n",
argv[1]);
exit(1);
}
printf("Number of elements stated in file: %u\n",size_from_file );
if (size_from_file > MAX_ELEMENTS) {
fprintf(stderr,
"This program can't handle an array of over %u elements.\n",
(unsigned int) MAX_ELEMENTS);
exit(1);
}
how_many_read = fread(input_data, sizeof(unsigned int), size_from_file, fp);
if (how_many_read != size_from_file) {
fprintf(stderr,
"Read error occurred before %u elements could be read from %s.\n",
size_from_file,
argv[1]);
exit(1);
}
/* We created this for loop instead of reusing the printf one below */
/* It seems more clear for us to use a temp array rather than */
/* directly copying from left to right into the correct positions. */
/* It may be wasteful but this section is much easily adapted to print */
/* for the other endian-ness. */
for(j = 0; j < size_from_file; j++)
{
b = input_data[j] & 0x000000ff;
b = b << 24;
input_temp[j] = input_temp[j] | b;
b = input_data[j] & 0x0000ff00;
b = b << 8;
input_temp[j] = input_temp[j] | b;
b = input_data[j] & 0x00ff0000;
b = b >> 8;
input_temp[j] = input_temp[j] | b;
b = input_data[j] & 0xff000000;
b = b >> 24;
input_temp[j] = input_temp[j] | b;
}
for (i = 0; i < size_from_file; i++)
printf("Element %d: %u\n", i, input_temp[i]); /*Change to input_data[i] for other endian case*/
if (fclose(fp) != 0) {
fprintf(stderr,
"Error detected closing %s; something weird happened.\n",
argv[1]);
exit(1);
}
return 0;
}