VC 读写二进制文件

VC 读写二进制文件,第1张

用c的话 fprint fscanf 等等 你可以看看书啊 就是文件那章的 。。。

若是c++的话。。如下

#include <fstream>

using namespace std

// AuthInfo 是自定义的 struct

struct AuthInfo auth_info

string susername, spassword

/* 写文件 */

// 清零

ZeroMemory ( &auth_info, sizeof ( auth_info ) )

susername = “tanggaowei@gmail.com”

spassword = “000000″

// 内存拷贝

memcpy(auth_info.username, susername.c_str(), susername.length())

memcpy(auth_info.password, spassword.c_str(), spassword.length())

// 定义打开输出流

ofstream fout(”mbc.dat”, ios::binary)

// 写入

fout.write((char *)(&auth_info), sizeof(auth_info))

// 关闭输出流

fout.close()

/* 读文件 */

ZeroMemory ( &auth_info, sizeof ( auth_info ) )

ifstream fin ( “mbc.dat”, ios::binary )

fin.read((char *)(&auth_info), sizeof(auth_info))

susername = auth_info.username

spassword = auth_info.password

ZeroMemory ( auth_info.username, 100 ) // AuthInfo.username[100]

ZeroMemory ( auth_info.password, 50 )// AuthInfo.password[50]

memcpy(auth_info.username, susername.c_str(), susername.length())

memcpy(auth_info.password, spassword.c_str(), spassword.length())

fin.close()

typedef struct xxx //定义一个你的数据类型

{

int a

byte b

rect c

}xxx,*lpxxx

你就可以在程序用这个类型了。

void readfile()

{

xxx m

CFile i

i.open( "你的文件 ",CFilemodeRead)

i.Read(&m,sizeof(xxx))

i.close

当然你也可以重复读

xxx m[10]

for (int y=0y <10y++)

{i.Read(&m[y],sizeof(xxx))}

}

首先需要完善EncTable里的字节内容。(我估计这个EncTable里的字节应该是256个不重复的字节,要不密文就还原不了了。)

const unsigned char EncTable[256] =

{

0x76,0x3F,0xD9,0xE4,0xBB,0x92,0xDF,0xF4,0xF2,0xAF,0x97,0x34,0xE7,0xA8,0x20,0xF3,

0xC3,0xBE,0xA1,0xB8,0x41,0x38,0x8B,0x59,0x26,0x94,0x74,0x96,0xA5,0xA6,0xC7,0xEA,

0x16,0x3C,0x4E,0x88,0xD3,0x19,0x75,0x9F,0x03,0x15,0x54,0x42,0x8A,0xD7,0xCC,0x5A,

0xD8,0xBC,0x43,0x00,0x5D,0xA2,0xF5,0xFA,0x40,0xC8,0x65,0xC2,0xCF,0x72,0xA4,0xFC,

0xFB,0x77,0x33,0xF8,0x1B,0xF9,0xB3,0x80,0xF7,0x3D,0x45,0x9C,0xAB,0xE8,0xE1,0x58,

0x3A,0xCE,0x2C

}

const int N=1024 //我每次从文件中读1024个字节进行转换,你可以根据需要适当调节。

// strHexPathName是你要转换的Hex文件路径字符串(包括路径,文件名和扩展名)

// strBinPathName是转换后的Bin文件路径字符串(包括路径,文件名和扩展名)

void Convert(LPCTSTR strHexPathName,LPCTSTR strBinPathName)

{

CFile fileHex

CFile fileBin

if(!fileHex.Open(strHexPathName,CFile::modeRead|CFile::typeBinary)){

return

}

if(!fileBin.Open(strBinPathName,CFile::modeCreate|CFile::modeWrite|CFile::typeBinary)){

fileHex.Close()

return

}

unsigned char pBufRead[N]={0}

unsigned char pBufWrite[N]={0}

int nReadLen=0

do{

nReadLen=fileHex.Read(pBufRead,N)

if(nReadLen>0){

Encrypt(pBufRead,pBufWrite,nReadLen)

}

fileBin.Write(pBufWrite,nReadLen)

}while(nReadLen>0)

fileBin.Close()

fileHex.Close()

}

void Encrypt(unsigned char* pInBuf,unsigned char* pOutBuf,int nLength)

{

for(int i=0i<nLengthi++){

pOutBuf[i]=EncTable[pInBuf[i]]

}

}


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/tougao/11943171.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-19
下一篇 2023-05-19

发表评论

登录后才能评论

评论列表(0条)

保存