简体   繁体   English

如何使用C语言将图像复制到目录

[英]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 瓦尔特

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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