All pastes #515160 Raw Edit

Unnamed

public text v1 · immutable
#515160 ·published 2007-05-28 02:20 UTC
rendered paste body
#include <stdio.h>

int copyfile(char *source,char *dest) {
  FILE *sourceFile;
  FILE *destinationFile;
  char *buffer;
  int n;
  int filesize;

  sourceFile = fopen(source, "rb");
  destinationFile = fopen(dest, "wb");

  fseek(sourceFile,0,SEEK_END);
  filesize = ftell(sourceFile);
  fseek(sourceFile,0,SEEK_SET);

  if(sourceFile==NULL) {
    printf("Error: can't access file.c.\n");
    return 1;
  }
  else if(destinationFile==NULL) {
    printf("Error: can't create file for writing.\n");
    return 1;
  }
  else {

    do {
         int maxbuffer = 524288;
         int readsize;
         
         // Make sure we don't exceed the maximum buffer size
         if (filesize > maxbuffer) {
            readsize = maxbuffer;
         } else {
            readsize = filesize;
         }
         filesize -= readsize;
         
         // Create the read buffer
         buffer = malloc(readsize);
         if (buffer == NULL) {
            printf("ERROR: Could not allocate the section data buffer. (%d)\n", readsize);
            return -1;
         }
         
         // Read in the data from the PBP
         if (fread(buffer, readsize, 1, sourceFile) < 0) {
            printf("ERROR: Could not read in the section data.\n");
            return -1;
         }
         
         // Write the contents of the buffer to the output file
         if (fwrite(buffer, readsize, 1, destinationFile) < 0) {
            printf("ERROR: Could not write out the section data.\n");
            return -1;
         }
         
         // Clean up the buffer
         if (filesize) free(buffer);
         
         // Repeat if we haven't finished writing the file
    } while (filesize);
    
    printf("%s\n", buffer);
    
    return 1;
  }
}

int main(void) {
    copyfile("file.c","file2.c");
}