简体   繁体   中英

How to copy a image to a Directory in C language

I was trying to copy a image file using file open and write file method, but enable to achieve the image... So please help me out with the code along with the header file required.

   char ch, source_file[20], target_file[20];
   FILE *source, *target;
   source = fopen("Source", "r");
   if( source == NULL )
   {
   printf("Press any key to exit...\n");
   }
   target = fopen("Destination", "w");
   if( target == NULL )
   {
   fclose(source);
   }

   while( ( ch = fgetc(source) ) != EOF )
   fputc(ch, target);

   printf("File copied successfully.\n");

   fclose(source);
   fclose(target);

I tried This....

Try:

FILE *source, *target;
int i;
source = fopen("Source", "rb"); 

if( source == NULL ) { printf("Press any key to exit...\n");} //exit(EXIT_FAILURE); 

fseek(source, 0, SEEK_END);
int length = ftell(source);

fseek(source, 0, SEEK_SET);
target = fopen("Destination", "wb"); 

if( target == NULL ) { fclose(source); } //exit(EXIT_FAILURE);

for(i = 0; i < length; i++){
    fputc(fgetc(source), target);
}

printf("File copied successfully.\n"); 
fclose(source); 
fclose(target);

valter

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM