Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. question about copy a picture with c [modified]

question about copy a picture with c [modified]

Scheduled Pinned Locked Moved C / C++ / MFC
questionhelp
5 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    Dengjin_CN
    wrote on last edited by
    #1

    I wrote the code below to copy a picture,and why it will fail if i change the mode of fdest to "ab"? "ab" means write and add to file,isn't it?what's the difference?

    #include<stdlib.h>
    #include<stdio.h>

    #define MAX 1024
    int
    main(int argc,int **argv){

    FILE \*fsource,\*fdest;
    size\_t in;
    int buf\[MAX\];
    fsource = fopen("F:\\\\a.jpg.rar","rb");
    fdest = fopen("D:\\\\a.jpg","wb");
    if(fsource == NULL || fdest == NULL)
    	perror("open error");
    
    while((in = fread(buf,sizeof(int),MAX,fsource)) != 0){
    	fwrite(buf,sizeof(int),in,fdest);
    	fflush(fdest);
    }
    
    fclose(fsource);
    fclose(fdest);
    
    return EXIT\_SUCCESS;
    

    }

    modified on Saturday, October 17, 2009 5:49 AM

    R P 2 Replies Last reply
    0
    • D Dengjin_CN

      I wrote the code below to copy a picture,and why it will fail if i change the mode of fdest to "ab"? "ab" means write and add to file,isn't it?what's the difference?

      #include<stdlib.h>
      #include<stdio.h>

      #define MAX 1024
      int
      main(int argc,int **argv){

      FILE \*fsource,\*fdest;
      size\_t in;
      int buf\[MAX\];
      fsource = fopen("F:\\\\a.jpg.rar","rb");
      fdest = fopen("D:\\\\a.jpg","wb");
      if(fsource == NULL || fdest == NULL)
      	perror("open error");
      
      while((in = fread(buf,sizeof(int),MAX,fsource)) != 0){
      	fwrite(buf,sizeof(int),in,fdest);
      	fflush(fdest);
      }
      
      fclose(fsource);
      fclose(fdest);
      
      return EXIT\_SUCCESS;
      

      }

      modified on Saturday, October 17, 2009 5:49 AM

      R Offline
      R Offline
      Rajesh R Subramanian
      wrote on last edited by
      #2

      What do you mean by "it will fail"? The file is not readable? The function itself fails to execute? Does the file exist while you are using the ab mode? In which case, the contents will simply be appended, and this might render the file unusable.

      Dengjin_CN wrote:

      what's the difference?

      You could refer to the table here[^]

      “Follow your bliss.” – Joseph Campbell

      D 1 Reply Last reply
      0
      • R Rajesh R Subramanian

        What do you mean by "it will fail"? The file is not readable? The function itself fails to execute? Does the file exist while you are using the ab mode? In which case, the contents will simply be appended, and this might render the file unusable.

        Dengjin_CN wrote:

        what's the difference?

        You could refer to the table here[^]

        “Follow your bliss.” – Joseph Campbell

        D Offline
        D Offline
        Dengjin_CN
        wrote on last edited by
        #3

        thanks for your reply. "it fails" means the file was created but was empty.I realized that the file d:\\a.jpg doesn't exist there before the program runs,now i created it then execute the program,it successed. however,i can't understand my book says "a" will create a file when the file doesn't exist,campare with "w",:confused:why it produces different result?

        L 1 Reply Last reply
        0
        • D Dengjin_CN

          thanks for your reply. "it fails" means the file was created but was empty.I realized that the file d:\\a.jpg doesn't exist there before the program runs,now i created it then execute the program,it successed. however,i can't understand my book says "a" will create a file when the file doesn't exist,campare with "w",:confused:why it produces different result?

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          I just tried your program and it works correctly in both cases. If you use the specification "wb" then it will always create a new file. If you use "ab" it will do the same, except if the file already exists then it will add to the existing file. For example if you write 100 bytes with "wb" your file will always be 100 bytes long. But if you write 100 with "wb" and then another 100 with "ab" it will be 200 bytes long.

          1 Reply Last reply
          0
          • D Dengjin_CN

            I wrote the code below to copy a picture,and why it will fail if i change the mode of fdest to "ab"? "ab" means write and add to file,isn't it?what's the difference?

            #include<stdlib.h>
            #include<stdio.h>

            #define MAX 1024
            int
            main(int argc,int **argv){

            FILE \*fsource,\*fdest;
            size\_t in;
            int buf\[MAX\];
            fsource = fopen("F:\\\\a.jpg.rar","rb");
            fdest = fopen("D:\\\\a.jpg","wb");
            if(fsource == NULL || fdest == NULL)
            	perror("open error");
            
            while((in = fread(buf,sizeof(int),MAX,fsource)) != 0){
            	fwrite(buf,sizeof(int),in,fdest);
            	fflush(fdest);
            }
            
            fclose(fsource);
            fclose(fdest);
            
            return EXIT\_SUCCESS;
            

            }

            modified on Saturday, October 17, 2009 5:49 AM

            P Offline
            P Offline
            Patcher32
            wrote on last edited by
            #5

            wb = open new binary file for writing, if file exists with same name, it will be truncated to 0 bytes ab = open new binary file for writing, if file exists with same name, new data would be written at end of file. This may lead to undesired results if file already exists. you should check if a file exists before opening it in wb mode :

            #include<stdlib.h>

            #include<stdio.h>
            #define MAX 1024

            int main(int argc,int **argv){

            FILE \*fsource,\*fdest;
            size\_t in;
            int buf\[MAX\];
                char c = 'n';
            fsource = fopen("F:\\\\a.jpg.rar","rb");
                //-----------Check if file exists---------
                fdest = fopen("D:\\\\a.jpg","r");
                if(fdest!=NULL){
                   fclose(fdest);
                   printf("Destination file already exists, do you want to overwrite?\\t");
                   scanf("%c",&c);
                   if(c!='Y' && c!='y'){
                        fclose(fSource);
                        return 1;
                   }  
                }
                //---------------------------------------
            fdest = fopen("D:\\\\a.jpg","wb");
            if(fsource == NULL || fdest == NULL)
            	perror("open error");
            
            while((in = fread(buf,sizeof(int),MAX,fsource)) != 0){
            	fwrite(buf,sizeof(int),in,fdest);
            	fflush(fdest);
            }
            
            fclose(fsource);
            fclose(fdest);
            
            return EXIT\_SUCCESS;
            

            }

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups