这是一个复制文件的简单程序,大家帮忙测试一下这种复制方法对于复制后的文件,是否会造成该文件末多个字节,谢谢。我只是想确定一下,这种问题是否与不同的计算机有关。 #include <stdio.h>
#define BUF_SIZE 1024
typedef unsigned char byte;
static long filelen(FILE *fp);
int main(int argc,char *argv[]) { if(argc < 3) { printf("Usage: %s <source> <destination>\n",argv[0]); return 0; } else { FILE *fin = NULL,*fout = NULL; byte buffer[BUF_SIZE] = {0}; int amount = 0, reminder = 0,i; long fl = 0; fin = fopen(argv[1],"rb"); if(!fin) { printf("Cannot open file %s\n",argv[1]); return 0; } fl = filelen(fin); fout = fopen(argv[2],"wb"); amount = fl / BUF_SIZE; reminder = fl % BUF_SIZE; for(i = 0; i < amount; i++) { fread(buffer,sizeof(byte) * BUF_SIZE,1,fin); fwrite(buffer,sizeof(byte) * BUF_SIZE,1,fout); } fread(buffer,sizeof(byte) * reminder,1,fin); fwrite(buffer,sizeof(byte) * reminder,1,fout); fclose(fin); fclose(fout); printf("File copy complete!\n"); return 0; } }
static long filelen(FILE *fp) { long rec; fseek(fp,0L,SEEK_END); rec = ftell(fp); fseek(fp,0L,SEEK_SET); return rec; }
[此贴子已经被作者于2005-9-9 21:17:10编辑过]