uint8_t File_Name[128] = {0}; uint8_t File_Name2[128] = {0}; #define ROW_SIZE 128
/* 获取配置信息 */ uint8_t Get_File_Config(void) { FILE *Fg; uint8_t i,j=0,str=0; uint8_t Row_Val[ROW_SIZE+1]; Fg = fopen(".\\Config.ini","r"); if(Fg == NULL) return 0; //while(fgets(Row_Val,ROW_SIZE,Fg)!=NULL) while(fread(Row_Val,1,ROW_SIZE,Fg) != 0) { for(i=0;i<ROW_SIZE;i++) { if(Row_Val[i] == '"') { str++; j=0; continue; } if(str == 1) File_Name[j++]=Row_Val[i]; if(str == 3) File_Name2[j++]=Row_Val[i]; } memset(Row_Val,0,sizeof(Row_Val)); } fclose(Fg); return 1; }
uint32_t Read_FileSize(uint8_t *file) { uint32_t sz = 0; FILE *Fp; Fp = fopen(file,"r"); if(Fp == NULL) { printf("文件打开失败!\n\t提示:请检查路径!\n"); scanf("%d",&sz); return 0; } fseek(Fp, 0L, SEEK_END); sz = ftell(Fp); fclose(Fp); return sz; }
void main(void) { uint8_t Read_Buf[128]={0}; uint8_t Write_Buf[600]={0}; uint32_t File_Size = 0; uint32_t A = 0; uint32_t i = 0,n = 0,j = 0,k = 0; FILE *Fp1; FILE *Fp2; if(Get_File_Config() == 1) { File_Size = Read_FileSize(File_Name); printf("文件大小:%d Byte\n",File_Size); } if(File_Size > (1024*1024)) printf("文件大小超过范围\n");
Fp1=fopen(File_Name,"rb"); Fp2=fopen(File_Name2,"wb"); if(Fp1==NULL) { printf("文件1打开失败!\n\t 路径:%s\n",File_Name); scanf("%d",&i); return ; } if(Fp2==NULL) { printf("文件2打开/创建失败!\n\t 路径:%s\n",File_Name2); scanf("%d",&i); return ; } for(i=0;i<File_Size;i=i+n) { if(i == 0) { memcpy(Write_Buf,"File_Code",9); k = k+9; Write_Buf[k++] = '\n'; Write_Buf[k++] = '{'; } if(i%128 == 0) n = fread(Read_Buf,1,ROW_SIZE,Fp1); for(j=0;j<n;j++) { if(j%16 == 0) { Write_Buf[k++] = '\n'; Write_Buf[k++] = '\t'; } if(Read_Buf[j] <= 0x0F) sprintf( &Write_Buf[k], "0x0%1x",Read_Buf[j]); else sprintf( &Write_Buf[k], "0x%x",Read_Buf[j]); k = k + 4; if((i+j) == (File_Size-1)) { Write_Buf[k++] = '\n'; Write_Buf[k++] = '}'; Write_Buf[k++] = ';'; } else Write_Buf[k++] = ','; } A = A + fwrite(Write_Buf,1,k,Fp2); k = 0; } printf("写入数据大小: %d Byte\n",A); fclose(Fp1); fclose(Fp2); }
/*********************************************/
File1=".\File1.bin" File2=".\File2.xbin"