【发布时间】:2019-09-24 13:53:54
【问题描述】:
我正在尝试从二进制文件中读取数据。一个数据块的长度为 76 字节(这随块中间的 2 字节“主要数据项”的数量而变化)。第一个数据是4个字节,第二个是4个字节,然后是一堆2字节的主要数据项,最后是2个2字节的数据。
基于this Delphi sample,我学会了如何使用以下代码读取文件:
short AShortInt; // 16 bits
int AInteger; // 32 bits
try
{
infile=new TFileStream(myfile,fmOpenRead); // myfile is binary
BR = new TBinaryReader(infile, TEncoding::Unicode, false);
for (int rows = 0; rows < 5; rows++) { // just read the first 5 blocks of data for testing
AInteger = BR->ReadInt32(); // read first two 4 byte integers for this block
AInteger = BR->ReadInt32();
for (int i = 0; i < 32; i++) { // now read the 32 2-byte integers from this block
AShortInt = BR->ReadInt16();
}
AShortInt = BR->ReadInt16(); // read next to last 2-byte int
AShortInt = BR->ReadInt16(); // read the last 2-byte int
}
delete infile;
delete BR;
Close();
}
catch(...)
{
delete infile; // closes the file, doesn't delete it.
delete BR;
ShowMessage("Can't open file!");
Close();
}
但是,我想做的是使用一个 76 字节宽的缓冲区来读取整个块,然后从该缓冲区中挑选各种数据。我将下面的代码based on this question 放在一起,我可以将整个数据块读入缓冲区。
UnicodeString myfile = System::Ioutils::TPath::Combine(System::Ioutils::TPath::GetDocumentsPath(), "binaryCOM.dat");
TFileStream*infile=0;
try
{
infile=new TFileStream(myfile,fmOpenRead);
const int bufsize=76;
char*buf=new char[bufsize];
int a = 0;
while(int bytesread=infile->Read(buf,bufsize)) {
a++; // just a place to break on Run to Cursor
}
delete[]buf;
}
catch(...)
{
delete infile;
ShowMessage("Can't open file!");
Close();
}
但我不知道如何从缓冲区中的字节中拼凑出子集。有没有办法连接 字节?所以我可以将一个数据块读入一个 76 字节的缓冲区,然后在下面做这样的事情?
unsigned int FirstDatum = buf[0]+buf[1]+buf[2]+buf[3]; // concatenate the 4 bytes for the first piece of data
这将是一个在 C++Builder 10.3.2 中内置的适用于 Win32、iOS 和 Android 的 FMX 应用程序。
【问题讨论】:
-
声明一个记录结构,其中包含您喜欢的整数和短整数。一次读取所有 76 个字节到该结构的一个实例,然后在声明它们时使用它们的名称引用各个字段。 (我只是不确定你提到的平台上的字节序)
-
谢谢汤姆。抱歉,我忘了提一个细节,在块的开始和结束之间会有可变数量的“2 字节主要数据项”。所以块不会总是 76 字节。有没有办法在运行时创建结构,以便满足可变性的需求?
-
您必须使用动态分配的数组来读取整数。话虽如此,您仍然可以使用
TBinaryReader读取单个整数,只需将数据块读入TMemoryStream并将其用作阅读器的输入数据而不是TFileStream。 -
感谢 Remy,我在答案中发布了我修改后的代码以保持问题清洁。这非常有效,因为可变长度的“主要数据”始终是 2 字节有符号短整数。
标签: firemonkey c++builder c++builder-10.3-rio