將1.bmp文件之?dāng)?shù)據(jù)部分抽取至文本文件bmp.txt中。
保存格式:
{0xA8, 0x88, 0x0C, 0x14, ..}
係字符,,使用時(shí)注意轉(zhuǎn)換,。
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////// main.cpp //////////////////////////////////
#include "main.h"
void main(void) { printf("%s/n", "START..."); HANDLE hMyFile; DWORD dwFileSize, dwReaded; BITMAPFILEHEADER * pBmp; //HBITMAP hBmp;
hMyFile = CreateFile("1.bmp", GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_FLAG_SEQUENTIAL_SCAN, NULL);
if(hMyFile == INVALID_HANDLE_VALUE) //if((hMyFile = fopen("C://1.bmp", "rb")) == INVALID_HANDLE_VALUE) { printf("Open de file error!/n"); return; } dwFileSize = GetFileSize(hMyFile, NULL);
pBmp = (BITMAPFILEHEADER *)malloc(dwFileSize);
if(!pBmp) { printf("malloc失敗!/n"); CloseHandle(hMyFile); return; }
if(!(ReadFile(hMyFile, pBmp, dwFileSize, &dwReaded, NULL) || (dwReaded != dwFileSize) || (pBmp -> bfType != *(WORD *)"BM") || pBmp -> bfSize != dwFileSize)) { printf("ReadFile錯(cuò)誤!/n"); free(pBmp); return; } printf("here/n"); printf("pBmp->bfType = %c/n",pBmp->bfType);
FILE * fpTxt = fopen("bmp.txt","ab"); if( fpTxt == NULL) { printf("Txt文件創(chuàng)建失敗,!/n"); return; }
//分析並寫(xiě)入文件 BYTE * bpCurrent; bpCurrent = (BYTE *)pBmp + pBmp->bfOffBits; printf("pBmp = %p, bpC = %p/n", pBmp, bpCurrent);
fprintf(fpTxt, "{"); for(UINT j = 0; j <= (UINT)dwFileSize - 58 + 1 +1; j++) { printf("%4x",*bpCurrent); fprintf(fpTxt, "0x%02x,",*bpCurrent); bpCurrent++; } fprintf(fpTxt, "0x%02x",*bpCurrent);
fprintf(fpTxt, "}/r/n");
fclose(fpTxt); free(pBmp);
// printf("/n%s/n", "END!");
}
|