C++ 在C+中读取/写入结构化二进制文件+;
我想读\写一个具有以下结构的二进制文件: 该文件由“记录”组成。每个“记录”都有以下结构:我将使用第一个记录作为示例C++ 在C+中读取/写入结构化二进制文件+;,c++,visual-studio-2010,io,binaryfiles,C++,Visual Studio 2010,Io,Binaryfiles,我想读\写一个具有以下结构的二进制文件: 该文件由“记录”组成。每个“记录”都有以下结构:我将使用第一个记录作为示例 (红色)开始字节:0x5A(始终为1字节,固定值0x5A) (绿色)长度字节:0x00 0x16(始终为2个字节,值可以更改 从“0x00 0x02”到“0xFF 0xFF”) (蓝色)内容:由的十进制值指示的字节数 长度字段减2 在本例中,LENGHT字段值为22(0x00 0x16转换为十进制),因此内容将包含20(22-2)个字节。 我的目标是逐个读取每条记录,并将其
- (红色)开始字节:0x5A(始终为1字节,固定值0x5A)
- (绿色)长度字节:0x00 0x16(始终为2个字节,值可以更改 从“0x00 0x02”到“0xFF 0xFF”)
- (蓝色)内容:由的十进制值指示的字节数 长度字段减2
std::ios\u base::binary
)中打开文件
非常相似,但如果无法提取字符,则返回eof
,而不是-1
。并将使您能够将给定数量的字节读入一个值。注意,您熟悉的一些类型(<代码>字节<代码> > <代码>类型[]/COD>)不存在于C++中,也不存在不同的工作。对于后者,您可以使用std::vector
,但您需要自己定义字节。您需要使用并在(std::ios\u base::binary
)中打开文件
非常相似,但如果无法提取字符,则返回eof
,而不是-1
。并将使您能够将给定数量的字节读入一个值。注意,您熟悉的一些类型(<代码>字节<代码> > <代码>类型[]/COD>)不存在于C++中,也不存在不同的工作。对于后者,您可以使用std::vector
,但您需要自己定义byte
。我想我应该按照以下顺序做一些事情:
struct record {
static const int start = '\x5a';
std::vector<char> data; // you might prefer unsigned char.
};
std::istream &operator>>(std::istream &is, record &r) {
char ch;
short len;
is.get(ch);
verify(ch == record::start);
is.read((char *)&len, sizeof(len));
r.data.resize(len);
is.read(&r.data[0], len);
return is;
}
std::ostream &operator<<(std::ostream &os, record const &r) {
os << record::start;
short len = (short)r.data.size();
os.write((char *)&len, sizeof(len));
os.write(&r.data[0], len);
return os;
}
然后,要处理文件,我们将使用如下代码:
std::transform(std::istream_iterator<record>(infile),
std::istream_iterator<record>(),
std::ostream_iterator<record>(outfile, ""),
process_record());
std::transform(std::istream_迭代器(infle),
std::istream_迭代器(),
std::ostream_迭代器(outfile,“”),
进程_记录());
[注意:为了简洁起见,我在这里使用了C风格的强制转换,但在实际代码中,我可能会使用静态\u强制转换
s。]我想我应该按照以下顺序做一些事情:
struct record {
static const int start = '\x5a';
std::vector<char> data; // you might prefer unsigned char.
};
std::istream &operator>>(std::istream &is, record &r) {
char ch;
short len;
is.get(ch);
verify(ch == record::start);
is.read((char *)&len, sizeof(len));
r.data.resize(len);
is.read(&r.data[0], len);
return is;
}
std::ostream &operator<<(std::ostream &os, record const &r) {
os << record::start;
short len = (short)r.data.size();
os.write((char *)&len, sizeof(len));
os.write(&r.data[0], len);
return os;
}
然后,要处理文件,我们将使用如下代码:
std::transform(std::istream_iterator<record>(infile),
std::istream_iterator<record>(),
std::ostream_iterator<record>(outfile, ""),
process_record());
std::transform(std::istream_迭代器(infle),
std::istream_迭代器(),
std::ostream_迭代器(outfile,“”),
进程_记录());
[注意:为了简洁起见,我在这里使用了C风格的强制转换,但在实际代码中,我可能会使用静态\u强制转换
s。]好的,根据您的答案,我做了一些实验:
string sFile = "C:\Test.bin";
static const int START_BYTE = '\x5a';
char tempByte;
ifstream inputFile (sFile, ios::in);
inputFile.open( sFile, ios::binary );
while (!inputFile.eof())
{
inputFile.get(temptByte);
cout << "Value of Byte " << hex << static_cast<int>(tempByte) << " hexadecimal" << endl;
}
string sFile=“C:\Test.bin”;
静态常量int START_字节='\x5a';
字符tempByte;
ifstream输入文件(sFile,ios::in);
open(sFile,ios::binary);
而(!inputFile.eof())
{
获取(字节);
好的,根据你的回答,我做了一些实验:
string sFile = "C:\Test.bin";
static const int START_BYTE = '\x5a';
char tempByte;
ifstream inputFile (sFile, ios::in);
inputFile.open( sFile, ios::binary );
while (!inputFile.eof())
{
inputFile.get(temptByte);
cout << "Value of Byte " << hex << static_cast<int>(tempByte) << " hexadecimal" << endl;
}
string sFile=“C:\Test.bin”;
静态常量int START_字节='\x5a';
字符tempByte;
ifstream输入文件(sFile,ios::in);
open(sFile,ios::binary);
而(!inputFile.eof())
{
获取(字节);
cout谢谢大家这是我能够开发的解决方案:
// TestCPP003.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "boost\program_options.hpp"
namespace po = boost::program_options;
#include "Util.h"
using namespace std;
int main(int argc, char* argv[])
{
po::options_description desc("Allowed options");
desc.add_options()
("h", "produce help message")
("i", po::value<string>(), "input file")
("o", po::value<string>(), "output file");
po::variables_map vm;
po::store(po::parse_command_line(argc, argv, desc), vm);
if (vm.count("h"))
{
cout << desc << endl;
return 0;
}
po::notify(vm);
if (vm.count("i"))
{
cout << vm["i"].as<string>() << "\n";
}
string sInputFile = vm["i"].as<string>();
if (fileExists(sInputFile.c_str()))
{
cout << "file exists: <" << sInputFile << ">" << endl;
}
else
{
cout << "file not exists: <" << sInputFile << ">" << endl;
cout << "RETURN CODE: 8" << endl;
return 8;
}
string sOutputFile = vm["o"].as<string>();
static const int START_BYTE = '\x5a';
static const int AFP_RECORD_HEADER_SIZE = 1;
static const int AFP_RECORD_LENGTH_SIZE = 2;
char * afpHeaderBlock = new char[1];
char * afpLengthBlock;
unsigned int afpRecordLength = 0;
char * afpContentBlock;
ifstream inputStream(sInputFile, ios::in|ios::binary);
ofstream outputStream(sOutputFile, ios::out|ios::binary);
if (inputStream.is_open() && outputStream.is_open())
{
while (inputStream.read(afpHeaderBlock, AFP_RECORD_HEADER_SIZE))
{
//cout << ToHex(string(afpHeaderBlock, AFP_RECORD_HEADER_SIZE), true) << endl;
if (START_BYTE == afpHeaderBlock[0])
{
cout << "0x5A Found!" << endl;
}
else
{
cout << "0x5A not Found! - AFP Error" << endl;
}
outputStream.write(afpHeaderBlock, AFP_RECORD_HEADER_SIZE);
afpLengthBlock = new char[AFP_RECORD_LENGTH_SIZE];
afpRecordLength = 0;
inputStream.read(afpLengthBlock, AFP_RECORD_LENGTH_SIZE);
//cout << ToHex(string(afpLengthBlock, AFP_RECORD_LENGTH_SIZE), true) << endl;
afpRecordLength = (afpRecordLength << 8) + static_cast<const unsigned char&>(afpLengthBlock[0]);
afpRecordLength = (afpRecordLength << 8) + static_cast<const unsigned char&>(afpLengthBlock[1]);
//cout << "AFP Record Length: " << afpRecordLength << endl;
outputStream.write(afpLengthBlock, AFP_RECORD_LENGTH_SIZE);
afpContentBlock = new char[afpRecordLength - AFP_RECORD_LENGTH_SIZE];
inputStream.read (afpContentBlock, afpRecordLength - AFP_RECORD_LENGTH_SIZE);
outputStream.write(afpContentBlock, afpRecordLength - AFP_RECORD_LENGTH_SIZE);
}
inputStream.close();
outputStream.flush();
outputStream.close();
}
cout << "RETURN CODE: 0" << endl;
return 0;
}
//TestCPP003.cpp:定义控制台应用程序的入口点。
//
#包括“stdafx.h”
#包括“boost\program\u options.hpp”
名称空间po=boost::program\u选项;
#包括“Util.h”
使用名称空间std;
int main(int argc,char*argv[])
{
采购订单:选项描述描述(“允许选项”);
说明添加选项()
(“h”,“生成帮助消息”)
(“i”,po::value(),“输入文件”)
(“o”,po::value(),“输出文件”);
变量映射虚拟机;
po::store(po::parse_命令行(argc、argv、desc)、vm);
如果(vm.count(“h”))
{
cout谢谢大家这是我能够开发的解决方案:
// TestCPP003.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "boost\program_options.hpp"
namespace po = boost::program_options;
#include "Util.h"
using namespace std;
int main(int argc, char* argv[])
{
po::options_description desc("Allowed options");
desc.add_options()
("h", "produce help message")
("i", po::value<string>(), "input file")
("o", po::value<string>(), "output file");
po::variables_map vm;
po::store(po::parse_command_line(argc, argv, desc), vm);
if (vm.count("h"))
{
cout << desc << endl;
return 0;
}
po::notify(vm);
if (vm.count("i"))
{
cout << vm["i"].as<string>() << "\n";
}
string sInputFile = vm["i"].as<string>();
if (fileExists(sInputFile.c_str()))
{
cout << "file exists: <" << sInputFile << ">" << endl;
}
else
{
cout << "file not exists: <" << sInputFile << ">" << endl;
cout << "RETURN CODE: 8" << endl;
return 8;
}
string sOutputFile = vm["o"].as<string>();
static const int START_BYTE = '\x5a';
static const int AFP_RECORD_HEADER_SIZE = 1;
static const int AFP_RECORD_LENGTH_SIZE = 2;
char * afpHeaderBlock = new char[1];
char * afpLengthBlock;
unsigned int afpRecordLength = 0;
char * afpContentBlock;
ifstream inputStream(sInputFile, ios::in|ios::binary);
ofstream outputStream(sOutputFile, ios::out|ios::binary);
if (inputStream.is_open() && outputStream.is_open())
{
while (inputStream.read(afpHeaderBlock, AFP_RECORD_HEADER_SIZE))
{
//cout << ToHex(string(afpHeaderBlock, AFP_RECORD_HEADER_SIZE), true) << endl;
if (START_BYTE == afpHeaderBlock[0])
{
cout << "0x5A Found!" << endl;
}
else
{
cout << "0x5A not Found! - AFP Error" << endl;
}
outputStream.write(afpHeaderBlock, AFP_RECORD_HEADER_SIZE);
afpLengthBlock = new char[AFP_RECORD_LENGTH_SIZE];
afpRecordLength = 0;
inputStream.read(afpLengthBlock, AFP_RECORD_LENGTH_SIZE);
//cout << ToHex(string(afpLengthBlock, AFP_RECORD_LENGTH_SIZE), true) << endl;
afpRecordLength = (afpRecordLength << 8) + static_cast<const unsigned char&>(afpLengthBlock[0]);
afpRecordLength = (afpRecordLength << 8) + static_cast<const unsigned char&>(afpLengthBlock[1]);
//cout << "AFP Record Length: " << afpRecordLength << endl;
outputStream.write(afpLengthBlock, AFP_RECORD_LENGTH_SIZE);
afpContentBlock = new char[afpRecordLength - AFP_RECORD_LENGTH_SIZE];
inputStream.read (afpContentBlock, afpRecordLength - AFP_RECORD_LENGTH_SIZE);
outputStream.write(afpContentBlock, afpRecordLength - AFP_RECORD_LENGTH_SIZE);
}
inputStream.close();
outputStream.flush();
outputStream.close();
}
cout << "RETURN CODE: 0" << endl;
return 0;
}
//TestCPP003.cpp:定义控制台应用程序的入口点。
//
#包括“stdafx.h”
#包括“boost\program\u options.hpp”
名称空间po=boost::program\u选项;
#包括“Util.h”
使用名称空间std;
int main(int argc,char*argv[])
{
采购订单:选项描述描述(“允许选项”);
说明添加选项()
(“h”,“生成帮助消息”)
(“i”,po::value(),“输入文件”)
(“o”,po::value(),“输出文件”);
变量映射虚拟机;
po::store(po::parse_命令行(argc、argv、desc)、vm);
如果(vm.count(“h”))
{
库特