文件数据拷贝问题【鸡毛信】
// wav3.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
int _tmain(int argc, _TCHAR* argv[])
{
long t=0;
FILE *in,*out;
char wav[129559]={};
/*if(argc!=3){
printf("\n Usage: Hcopy sourcefile targetfile.\n");
exit(1);
system("pause");
}*/
if((in=fopen("TEST.wav","rb"))==NULL)
{
printf("\n Cannot open the source file.\n");
exit(2);
system("pause");
}
if((out=fopen("wav.h","wb"))==NULL){
printf("\n Cannot open the targetfile.\n");
exit(3);
system("pause");
}
/* start copy */
printf("wav[]={");
fseek(in,585L,SEEK_SET);
while(!feof(in))
{
fread("TEST.wav",sizeof(char),129559,in);
fwrite("wav.h",sizeof(char),129559,out);
} //putc(getc(in),out);printf("}");
fclose(in);
fclose(out);
return 0;
system("pause");
}
我想得到二进制数据,可红色部分不知道为什么不能实现;紫色部分确实能将数据拷贝,可拷贝的是文本文件,请大虾指点一二,不胜感激
[解决办法]
先fread读到一个unsigned char数组里面然后再将这个数组fwrite到目标文件。你这里根本都用错了。
unsigned char wav[129559]={0};
fread(wav,sizeof(wav),1,in);
fwrite(wav,sizeof(wav),1,out);
[解决办法]
把数组的大小改小点,然后分多次读写
[解决办法]
// wav3.cpp : 定义控制台应用程序的入口点。//#include "stdafx.h"int _tmain(int argc, _TCHAR* argv[]) { long t=0; int c; FILE *in,*out; static char wav[129559]={}; /*if(argc!=3){ printf("\n Usage: Hcopy sourcefile targetfile.\n"); exit(1); system("pause"); }*/ if((in=fopen("TEST.wav","rb"))==NULL) { printf("\n Cannot open the file TEST.wav.\n"); system("pause"); exit(2); } if((out=fopen("wav.h","w"))==NULL) { printf("\n Cannot open the target file wav.h.\n"); system("pause"); exit(3); } /* start copy */ fprintf(out,"wav[]={\n"); fseek(in,585L,SEEK_SET); t=0; while(1) { t++; if (t>129559) break; c=fgetc(in); if (EOF==c) break; fprintf(out,"0x%02x,",(unsigned int)(c&0xFFu)); if (0==t%8) fprintf(out,"\n"); } //putc(getc(in),out); fprintf(out,"\n};\n"); fclose(in); fclose(out); return 0; system("pause");}