File Copy Program in C

Started by Kalyan, Apr 06, 2008, 06:30 PM

Previous topic - Next topic

Kalyan

File Copy Program in C

/* WARNING: be sure to edit the line 23 and make "text the file you
want to copy, be sure to leave the file name in quotes, filename can me
relative or absolute
*/

#include <stdio.h>


int main()
{
       char c[100];
       FILE *inFile;
       FILE *outFile;
       char sourceFile;
       char destFile;
       int Byte;
       int i;


//       printf("Enter the File Name to read: ");
//       scanf("%s",&sourceFile);
       printf("Enter the File Name to write to: ");
       scanf("%s",&destFile);

       inFile = fopen("text", "rb");
       /*open a text file for reading in binary */
       outFile = fopen(&destFile, "wb");
       /*open a text file for writing in binary*/

       if(inFile==NULL)
       {
       /*if pointer to inFile is a null pointer,
         return an error and display msg */

              printf("Error: Can't Open sourceFile\n");
              /*be sure not invoke fclose, because
                you can't pass a NULL pointer to it
              */
              return 1; //return 1 for error;
       }
       if(outFile==NULL)
       {
              printf("Error: Can't Open DestFile\n");
              return 1;
       }
       else
       {
              printf("File Opened Successfully.");
              printf("\nContents:\n");

              while(1)
              {
                     if(Byte!=EOF)
                     {
                     Byte=fgetc(inFile);
                     printf("%d",Byte);
                     fputc(Byte,outFile);
                     }
                     else
                     {
                     break;
                     }
              }
       /*       for(i=0;c!='\0';i++)
              {
                     newByte = *c + 25;
                     fputc(newByte,outFile);
              }
*/
       printf("\n\n Now Closing Files...\n");
       fclose(inFile);
       fclose(outFile);
       return 0;
       }
}