//* Reading bytes from one file and copying all byte chunk by byte chunk to another file *

#ifdef _MSC_VER // for MS Visual Studio compilers - to allow "classic" insecure printf and scanf #define _CRT_SECURE_NO_WARNINGS #endif #include <stdio.h> #include <string.h> #define buffer_size (512) int main() { // declare the file handles FILE *inFile; FILE *outFile; // declare variables to hold the file names char inName[80]; char outName[80]; // prompting user for filenames printf("Read data from: "); scanf(" %79[^\n]s\n", inName); printf("Write data to: "); scanf(" %79[^\n]s\n", outName); // actual opening of the file for reading from inFile = fopen(inName, "rb"); // read in binary mode if ( inFile==0 ) { printf("ERROR: Cannot open file %s for reading.\n", inName); return(1); } // actual opening of the file for writing to outFile = fopen(outName, "wb"); // write in binary mode if ( outFile==0 ) { printf("ERROR: Cannot open file %s for writing.\n", outName); fclose(inFile); return(1); } // reading-writing loop for (;;) { char buffer[buffer_size]; int itemsread; itemsread = fread(buffer, 1, buffer_size, inFile); // Note: last read may have less than full buffer read if ( itemsread>0 ) fwrite(buffer, 1, itemsread, outFile); if ( feof(inFile) ) break; } // closing the files fclose(inFile); fclose(outFile); return(0); }