我打算在创建一个二进制文件前先向文件中的头部写入一段固定的十六进制数,如下面的示例,但实际执行结果不向预料的那样,文件最终长度就是到十六进制第一次出现
0x00的位置.
示例代码:
#include <stdio.h>
#include <stdlib.h>
#include <fstream.h> ofstream wfile;
CString filePath = "d:\\test.bin";
wfile.open(filePath, ios::out | ios::binary);
if(wfile.is_open() == 0)
return ; static char ch1[7] ={0x4D,0x00,0x4D,0xFF,0x4D,0x00,0x4D};
char * pCh = new char[7];
memcpy(pCh,ch1,7);
wfile.write(pCh,7);
/*
或for(INT i=0; i<7; i++)
{
wfile<<ch1[i];
}
*/
wfile.close();
delete[] pCh;
pCh = NULL;//写入的文件用Uedit32打开看第一个字节内容是正确的,是4D,文件也只有这一个字节,即文件长度为4D,
在编绎中查询内存,pCh首地址往后7个字节的内容也是正确的,只是没有完全写入文件
哪位高手指点一下如何作?定送100分
0x00的位置.
示例代码:
#include <stdio.h>
#include <stdlib.h>
#include <fstream.h> ofstream wfile;
CString filePath = "d:\\test.bin";
wfile.open(filePath, ios::out | ios::binary);
if(wfile.is_open() == 0)
return ; static char ch1[7] ={0x4D,0x00,0x4D,0xFF,0x4D,0x00,0x4D};
char * pCh = new char[7];
memcpy(pCh,ch1,7);
wfile.write(pCh,7);
/*
或for(INT i=0; i<7; i++)
{
wfile<<ch1[i];
}
*/
wfile.close();
delete[] pCh;
pCh = NULL;//写入的文件用Uedit32打开看第一个字节内容是正确的,是4D,文件也只有这一个字节,即文件长度为4D,
在编绎中查询内存,pCh首地址往后7个字节的内容也是正确的,只是没有完全写入文件
哪位高手指点一下如何作?定送100分
我的示例代码忽略讲了了实际程序中的一个细节即
上面的代码放在了另一个函数中
//参数ofstream wfile 主函数中已经被打开的文件流对象
//参数int headFormat 写入二进制文件头的格式类型号
BOOL CreateFileHead(ofstream wfile, int headFormat);在主函数
ofstream wfile;
CString filePath = "d:\\test.bin";
wfile.open(filePath, ios::out | ios::binary);
if(wfile.is_open() == 0)
return ;
CreateFileHead(wfile,enuType); .......再接着写头下面的数据内容
//写头的函数
BOOL CreateFileHead(ofstream wfile, int headFormat)
{
static char ch1[7] ={0x4D,0x00,0x4D,0xFF,0x4D,0x00,0x4D};
char * pCh = new char[7];
memcpy(pCh,ch1,7);
wfile.write(pCh,7); delete[] pCh;
pCh = NULL;}
刚才我又在一个EXE工程中测了一下也是正确的,但我的源码中一模一样的测试却不一样,唉急死人了