一个拷贝的小程序
#include <stdio.h> #include <string.h> #define BUF_SIZE 1000 int main() { FILE *fp_src,*fp_dst; size_t readed; char buf[BUF_SIZE]; memset(buf,0,BUF_SIZE); if((fp_src=fopen("/root/test","rb"))==NULL) { fprintf(stderr,"open file1 failure! "); } if((fp_dst=fopen("/root/test2","wb"))==NULL) { fprintf(stderr,"open file2 failure! "); } while (!feof(fp_src)) { readed=fread(buf,sizeof(char),BUF_SIZE,fp_src); if(fwrite(buf,sizeof(char),BUF_SIZE,fp_dst)<readed) { //write file error! exit fprintf(stderr,"write file2 fialure! "); return 0; } } fclose(fp_src); fclose(fp_dst); printf("copy test to test2 success! "); return 0; }