c语言实现读取二进制文件
string m_vImgPath = "E://ImgOCTBin//normal//171221123143_00009//s001.mtsd";
FILE *fp = NULL;
fp = fopen(StringAbout::toCharX(StringAbout::toCString(m_vImgPath)), "rb");
int num1 = 0;
fseek(fp, 0L, SEEK_END); /* 定位到文件末尾 */
num1 = ftell(fp); /* 得到文件大小 */
int imgDataH = 860;
int wid = num1 / imgDataH;
unsigned char* mdata;
mdata = (unsigned char*)malloc(num1 * sizeof(unsigned char));
/* 定位到文件开头 */
fseek(fp, 0L, SEEK_SET);
/* 一次性读取全部文件内容 */
fread(mdata, 1, num1, fp);
fclose(fp);
FILE *fp1;
fp1 = fopen("FILEmtsd.txt", "w");
fprintf(fp1, "w:%d,h:%d,c:%d,num1:%d\n", wid, imgDataH, c,num1);
for (int i = 0; i < num1; i++)
{
{
fprintf(fp1, "%hhu,", mdata[i]);
}
if ((i + 1) % (wid*c) == 0)
{
fprintf(fp1, "\n");
}
}
fclose(fp1);