複製二進制文件
回答
http://www.cs.bu.edu/teaching/c/file-io/intro/
#include <ctype.h>
#include <stdio.h>
#define BUFSIZE 1024
int main(int argc, char **argv)
{
charmybuf[BUFSIZE] = { 0 }, *p = NULL;
FILE *ifd = NULL, *ofd = NULL;
ifp = fopen(argv[1], 「r」);
ofp = fopen(argv[2], 「w」);
assert(ifp!=NULL);
assert(ofp!=NULL);
while ((n = fread(mybuf, sizeof(char), BUFSIZE ,ifd)) > 0)
{
fwrite(mybuf, sizeof(char),BUFSIZE,ofd);
}
fclose(ifd);
fclose(ofd);
return 0;
}
確保您打開與O_BINARY選項中的文件,如果你使用open()
和文件描述符,或用字母「B」的模式字符串,如果你使用fopen()
。請注意,除Windows之外,O_BINARY不是標準的;但是,你可以在Unix上將它對待(或定義它)爲0,一切都會好的。如果你使用的是純粹的Windows API,那麼確保你正在做同樣的事情 - 確保你指定該文件被視爲二進制文件。
'open'是一個POSIX函數,而Windows不是POSIX。 – 2011-03-10 18:02:30
你是什麼意思「最好的方式」?我覺得這是最簡單的方法......希望這是你的意思:)
fopen
輸入和輸出文件的二進制模式
FILE *exein, *exeout;
exein = fopen("filein.exe", "rb");
if (exein == NULL) {
/* handle error */
perror("file open for reading");
exit(EXIT_FAILURE);
}
exeout = fopen("fileout.exe", "wb");
if (exeout == NULL) {
/* handle error */
perror("file open for writing");
exit(EXIT_FAILURE);
}
fread
和fwrite
size_t n, m;
unsigned char buff[8192];
do {
n = fread(buff, 1, sizeof buff, exein);
if (n) m = fwrite(buff, 1, n, exeout);
else m = 0;
} while ((n > 0) && (n == m));
if (m) perror("copy");
最後關閉文件
if (fclose(exeout)) perror("close output file");
if (fclose(exein)) perror("close input file");
玩得開心!
Windows有一個CopyFile API(如果你不介意是平臺特定的)。有一點需要注意的是,確保您有權寫入目標區域。
您可以使用'dos.h'函數進行低級IO操作。以下代碼說明了它們的用法。希望這將有所幫助
#include<stdio.h>
#include<dos.h>
#include<FCNTL.H>
#include<SYS\STAT.H>
void main()
{
char s_file[100],d_file[100],buf[512];
short char copy='y';
int in_handle,out_handle,flg,len;
printf("\nEnter File Name : ");
fflush(stdin);
gets(s_file);
printf("\nEnter Destination File Name : ");
fflush(stdin);
gets(d_file);
// open file for reading
in_handle=open(s_file,O_RDONLY | O_BINARY);
if(in_handle<0)
{
printf("\nSource File not Found... ");
}
else
{
// open file for writing
out_handle=open(d_file,O_CREAT|O_WRONLY|O_BINARY,S_IWRITE);
while((len=read(in_handle,buf,512))>0)
{
flg=write(out_handle,buf,len);
if(flg==-1)
break;
}
if(flg==-1)
{
printf("Unable to Create");
}
else
{
printf("File Created");
}
}
if(!(in_handle<0))
close(in_handle);
if(!(out_handle<0));
close(out_handle);
}
#include<stdio.h>
#include<stdlib.h>
int main()
{
FILE *fp1,*fp2;
char c;
fp1=fopen("source file","rb");
if(fp1==NULL)
exit(1);
fp2=fopen("destination file","wb");
if(fp2==NULL)
exit(1);
while((c=fgetc(fp1))!=EOF)
fputc(c,fp2);
fclose(fp1);
fclose(fp2);
return 0;
}
破壞文件 – nithin 2016-03-06 16:10:30
- 1. postgres複製命令,二進制文件
- 2. 複製二進制文件的內容
- 3. 二進制「尾巴」文件
- 4. 二進制安裝文件
- 5. 寫入二進制文件
- 6. 二進制文件I/O
- 7. 編譯二進制文件
- 8. 閱讀二進制文件
- 9. 的Python:二進制文件
- 10. StringIO與二進制文件?
- 11. 二進制文件操作
- 12. C++二進制文件
- 13. InflateException:二進制XML文件
- 14. 寫二進制文件
- 15. 二進制文件大小
- 16. 二進制.dat文件
- 17. 二進制文件解釋
- 18. 文件到二進制流
- 19. 寫入二進制文件?
- 20. 修改二進制文件
- 21. PHP - wkhtmltopdf二進制文件
- 22. 寫入二進制文件
- 23. 閱讀二進制文件
- 24. WebSphere MQ二進制文件
- 25. Ruby grep二進制文件?
- 26. 寫入二進制文件
- 27. 十進制到二進制(二進制)
- 28. 哪裏samtools二進制文件複製到某些目錄
- 29. 如何在Python中複製二進制文件的塊?
- 30. 無法通過c程序複製二進制文件
你應該張貼你已經嘗試過什麼,所以我們可以得到怎樣幫助一個想法 - 這是大多數編程語言中一個非常簡單的操作。 – Jeff 2011-03-10 17:14:12