question about copy a picture with c [modified]
-
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
-
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
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
-
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
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?
-
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?
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.
-
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
wb
= open new binary file for writing, if file exists with same name, it will be truncated to 0 bytesab
= 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 inwb
mode :#include<stdlib.h>
#include<stdio.h>
#define MAX 1024int 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;
}