标准I/O小程序-文件拷贝
#include <stdio.h>
#include <string.h>
/**
*
* ./mycopy src des
*/
int main(int argc, char *argv[])
{
FILE *des, *src;
char buf[128];
int ret, i, j, count;
if (argc < 3) {
printf("The argc is wrong!\n");
return -1;
}
src = fopen(argv[1], "r");
if (src == NULL) {
printf("fopen %s fail.\n", argv[1]);
return -1;
}
des = fopen(argv[2], "w+");
if (des == NULL) {
printf("fopen %s fail.\n", argv[2]);
fclose(src);
return -1;
}
i = 0;
j = 0;
while ((count = fread(buf, 1, 128, src)) > 0) {
i++;
j+=strlen(buf);
fwrite(buf, 1, count, des);
if (count < 128) {
printf("fwrite over.\n");
break;
}
}
printf("i=%d\n", i);
printf("j=%d\n", j);
fseek(src, 0, SEEK_END);
printf("src long:%ld\n", ftell(src));
fseek(des, 0, SEEK_END);
printf("des long:%ld\n", ftell(des));
fclose(src);
fclose(des);
return 0;
}