Copying any file

___Radian_
___Radian_ used Ask the Experts™
on
how can i copy any file to another destination in C?.
.exe, .doc, whatever it may be?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
#include <sys/stat.h>
#include <stdio.h>
#include <stdlib.h>

void main()
{
 struct _stat buf
 int data,i,flen;
 char filename[30];                                      
 char fdata[50000];                    
 FILE *fp;                        

 getfile:
 printf("Enter filename to transmit as string\n");
 scanf("%s", &filename);

 flen = strlen(filename);                  
 if (flen > 30)
 {
     printf("Filename too large\n");
     goto getfile;
 }

 fp = fopen(filename,"r");              
           if ( (fp == NULL))
           {
                printf("Error in opening the file for reading\n");
               goto getfile;
           }
                data = getc( fp );
result = _stat( filename,&buf );
                   i=0;

     while (i !=fsize )
/* opening the file in binary read and binary write mode will allow you to copy any file */

#include <sys/stat.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void main()
{
 struct _stat buf;
 int data,flen,result;
 long int i,fsize;
 char filename[30];                                      
 char fdata[50000];                    
 FILE *fp;                        

 getfile:
 printf("Enter filename to transmit as string\n");
 scanf("%s", &filename);

 flen = strlen(filename);                  
 if (flen > 30)
 {
     printf("Filename too large\n");
     goto getfile;
 }

 fp = fopen(filename,"rb");     // binary read          
           if ( (fp == NULL))
           {
                printf("Error in opening the file for reading\n");
               goto getfile;
           }
                data = getc( fp );

              result = _stat(filename,&buf );
              fsize = buf.st_size;
                 printf("%li",fsize);
     
i=0;
data = getc( fp );

     while (i !=fsize )
         {
          fdata[i] = data;                          
            data = getc( fp );                  
          i++;
     }
           fclose(fp);

          printf("Enter name to copy file to ");
          scanf("%s",&filename);

    fp = fopen(filename,"wb");  // binary write

i=0;
          while(i != fsize)
          {
               putc(fdata[i], fp );          
            i++;
          }
       fclose(fp);

printf("File copied successfully\n");
}
excuse that first bit i posted that by mistake when i was writing the code lol

Commented:
#include<stdio.h>
main(int argc,char **argv) {
    FILE *ifp,*ofp;
    int data;  /* char is an 8bit integer */
    if(argc <3) {
        printf("\nuseage : %s <input file> <output file> \n",argv[0]);
        exit(0);
    }
if((ifp=(FILE *) fopen(argv[1],"rb"))==NULL) {
        printf("\nunable to open file : %s ",argv[1]);
        exit(0);
    }
    if((ofp=(FILE *) fopen(argv[2],"wb"))==NULL) {
        printf("\nunable to open file : %s ",argv[2]);
        exit(0);
    }
  while((data=fgetc(ifp))!=EOF)
        fputc(data,ofp);
    fputc(EOF,ofp);
    fclose(ifp); fclose(ofp);
}

Commented:
#include<stdio.h>
# include <process.h>
#include<string.h>
void main()
{
 char *str1;
 int i=0;
 strcpy(str1 ,"copy  ");
 printf("enter filename with path");
 scanf("%s",str1+5);
 i=strlen(str1);
printf("%d",i);
 printf("enter path with new file name to be created");

 strcat(str1+i, " ");
 scanf(" %s",str1+i+1);


  puts(str1);

 system(str1 );


   }

Execute from dos Prompt

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial