如何将程序运行时的信息打印到一个文件中
问题如题,比如,我写了个文件复制的程序,用命令行写的,假如说我要把运行时的出错信息、拷贝进度、文件信息打印到一个文件中要怎么写代码,还有就是,为什么我的代码编译通过了之后,我在dos界面的运行总是不对,这个运行命令改怎么写,我把代码贴出来,请大家帮看看,谢谢!程序代码:
#include<stdio.h> #include<stdlib.h> #include<string.h> #define BUFFER_SIZE 1024 int main(int argc,char * argv[]) { FILE *fFrom,*fTo; char buffer[BUFFER_SIZE]; size_t BytesFrom,BytesTo; int length=0; if(argc!=3) { printf("Usage:%s fFrom fTo",argv[0]); exit(0); } fFrom=fopen(argv[1],"r"); if(NULL==fFrom) { printf("Open file %s failed\n",argv[1]); exit(0); } fTo=fopen(argv[2],"wb+"); if(NULL==fTo) { printf("Open file %s failed\n",argv[2]); exit(0); } memset(buffer,0,BUFFER_SIZE); while(BytesFrom=fread(buffer,BUFFER_SIZE,1,fFrom)>0) { BytesTo=fwrite(buffer,BytesFrom,1,fTo); if(BytesFrom!=BytesTo) { printf("file copy failed,plaese try againt"); exit(0); } length+=BytesTo; printf("copying file...%d bytes copy\n",length); } fclose(fFrom); fclose(fTo); return 0; }