CITS2002 Systems Programming  
prev
CITS2002 CITS2002 schedule  

Reading and writing binary data structures, continued

When reading and writing arbitrary binary data, there is an important consideration - different hardware architectures (the computer's CPU and data circuitry) store and manipulate their data in different formats.

The result is that when binary data written via one architecture (such as an Intel Pentium) is read back on a different architecture (such as an IBM PowerPC), the "result" will be different!

Writing on a 32-bit Intel Pentium:


#include <stdio.h>

#define N     10

  int array[N];

  for(int n=0 ; n < N ; ++n) {
      array[n]  =  n;
  }

  fwrite(array, N, sizeof int, fp_out);

Reading on a 32-bit PowerPC:


#include <stdio.h>

#define N     10

  int array[N];

  fread(array, N, sizeof int, fp_in);  

  for(int n=0 ; n < N ; ++n) {
      printf("%i ", array[n]);
  }
  printf("\n");

Prints the output:
0 16777216 33554432 50331648 67108864 83886080 100663296 117440512 134217728 150994944

The problems of reading and writing of binary data, to and from different architectures and across networks, are discussed and solved in later units, such as CITS3002 Computer Networks.

 

Jonathan Swift's Gulliver's Travels, published in 1726, provided the earliest literary reference to computers, in which a machine would write books. This early attempt at artificial intelligence was characteristically marked by its inventor's call for public funding and the employment of student operators. Gulliver's diagram of the machine actually contained errors, these being either an attempt to protect his invention or the first computer hardware glitch.

The term endian is used because of an analogy with the story Gulliver's Travels, in which Swift imagined a never-ending fight between the kingdoms of the Big-Endians and the Little-Endians (whether you were Lilliputian or Brobdignagian), whose only difference is in where they crack open a hard-boiled egg.

 


CITS2002 Systems Programming, Lecture 7, p15, 12th August 2024.