Superiority of elegance


Original code

from a public domain code to crack encoded resources in Win95
unsigned char Data[100001];
main(...){
  int     size;
  FILE * fd;
     .... /* opening a file */
  size=0;
  while(!feof(fd)) {
    Data[size++]=fgetc(fd);
  }
  size--;
}

Q: what happens if the file turns out longer than 100001?



Rewritten code

 size = fread(Data,1,sizeof(Data)-1,fd);
Why didn't he write this one-liner in the first place?!




It is much faster, it is absolutely safe (there is not even a possibility of getting over Data's boundary), and it is only one line for God's sake. The guy who wrote the code seems to be smart and knowledgeable. He should have written that one line on an "autopilot". Come to think of it, there is little wonder then why M$ and other software companies goof it up all the time...



Next