#include<stdio.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
#include <unistd.h>

#define BUFLEN 4096

char* filename="myfile.txt"; /* Default filename */

int main( int argc, char* argv[] )
{
  int n;
  FILE * f;
  char buf[BUFLEN];

  if (argc>1) filename = argv[1];

  /* creat a new file */
  if ( (f= fopen(filename, "w")) == NULL)
  {
    printf("ERROR 1: can't create file\n");
    return 1;
  }

  /* Read from stdout, write to file */
  while ( (n=fread(buf,sizeof(char),BUFLEN,stdin)) > 0 ) {
    fwrite( buf, sizeof(char), n, f); /* Why do we use n and not BUFLEN? */
  }

  fclose(f);

  return 1;
}
