/* Name : Edison Chindrawaly
 * File : filecopy.cc
 * Date : 03/06/01
 * Subj : To investigate the relationship of buffer size
 *        and file copy implementation
 */

#include <unistd.h>
#include <iostream.h>
#include <fcntl.h> 
#include <stdio.h> 
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <fstream.h>
#include <sys/wait.h>
#include <sys/time.h>
#include <sys/types.h>

void main()
{
  FILE *fp1;

  //used in gettimeofday - to measure time
  struct timeval timebefore;
  struct timeval timeafter;

  if((fp1 = fopen("input1.txt","w+")) == NULL)
  {
   perror("Cannot open file1.\n");
   exit(1);
  }
 
//Generate 64k text file

  for(int i = 0; i<64000; i++)
  { 
    if(fputc('a',fp1) == EOF)
    {
     perror("Fail to write to file1.");
     exit(1);
    }
  }

  fclose(fp1);

//Open the files using file stream

  ifstream file1("input1.txt", ios::in|ios::binary);
  ofstream file2("copy1.txt", ios::out|ios::binary);

  int buffersize = 0;
  cout<<"Determine the buffer size: ";
  cin>>buffersize;
 
  char buffer[buffersize];

  gettimeofday(&timebefore, NULL);
  file1.read(buffer,buffersize);
  file2.write(buffer,buffersize);
  gettimeofday(&timeafter, NULL);

  long different = (timeafter.tv_usec - timebefore.tv_usec);

  cout<<"Time different in microsecond : "<<different<<endl;
  cout<<"Time different in second : " <<different/3600<<endl;
  cout<<"Time different in minute : " <<different/(3600*60)<<endl;
  file1.close();
  file2.close();

}





 

1