c++ - 将 ifstream 读取为 char* 以进行 int 和 string 转换
问题描述
我在解析具有以下内容的二进制数据文件时遇到了一些麻烦,该文件读取了第一行
int LineLength = 300;
int Offset= 4586;
ifstream ifn("BinaryFile.bin");
char* line = new char[LineLength];
ifn.seekg(Offset);
while (ifn.good())
{
ifn.read(line, LineLength);
}
for (int i = 0; i < LineLength; i++)
printf("%02X ", line[i]);
这是打印出来的:
00 FFFFFFDD 01 00 00 22 FFFFFFB0 01 00 03 4F 50 2D 31 01 31 32 33
当我使用十六进制编辑器查看原始数据时,它看起来像这样:
00 55 24 00 00 F4 BC 01 00 03 4F 50 2D 31 01 31 32 33
我真的不希望它改变数据,我需要将一些字节转换为整数,一些转换为字符串,还有一些最终会变成日期,但我遇到了一些真正的挑战。
string* DataType= new string[3]{ "int","int","string" };
int* StartingPosition =new int[3] { 1,10,11 };
int* ColumnWidth =new int[3] { 4,1,25 };
char* locale = setlocale(LC_ALL, "UTF-8");
for (string Name : DataType) {
std::vector<char> CurrentColumnBytes(ColumnWidth);
for (int C = StartingPosition; C < ColumnWidth + StartingPosition; ++C)
{
int Index = C - StartingPosition;
CurrentColumnBytes[Index] = line[C];
}
if (DataType == "int") {
// still working out how to get a little endian result from 2-4 bytes if I use char
// since it is combining bytes as shown above
}
if (DataType == "string") {
for (std::vector<char>::const_iterator i = CurrentColumnBytes.begin(); i != CurrentColumnBytes.end(); ++i)
std::cout << *i;
}
}
我尝试使用wchar_t
andwint_t
而不是char
作为类型。我wint_t
能够将多个字节转换为一个小字节序 int,但是我似乎无法从中得到一个字符串。
对于它的价值,我还尝试使用以下方法来获取整数,但不能获取字符串。
int LineLength = 300;
int counter = 0;
wchar_t* line = new wchar_t[LineLength];
while ((c = fgetwc("BinaryFile.bin")) != WEOF) {
line[counter] = c;
counter++;
if (counter > LineLength - 1) {
break;
}
}
解决方案
好的,所以问题是您需要将文件读入unsigned char vector
/数组。也不要使用fgetwc
,否则你只会得到每隔一个字节,并且它会检测一行中何时有多个空字节并将它们组合起来,这可能会影响你的计数。Ifstream
似乎没有一种简单的方法可以将文件的一部分读入unsigned char vector
int LineLength = 300;
int counter = 0;
std::vector<unsigned char> line(LineLength);
while ((c = fgetw("BinaryFile.bin")) != WEOF) {
line[counter] = static_cast<unsigned char>(c);
counter++;
if (counter > LineLength - 1) {
break;
}
}
然后,一旦将其放入向量中,就可以单独处理数据
for (string Name : DataType) { // here just looping over the columns that were defined in the JSON file - excluding some things that aren't relevant
{
char* locale = setlocale(LC_ALL, "UTF-8");
std::vector<unsigned char> CurrentColumnBytes(ColumnWidth);
for (int C = StartingPosition; C < ColumnWidth + StartingPosition; ++C)
{
int Index = C - StartingPosition;
CurrentColumnBytes[Index] = BufferedLine[C];
}
if (DataType == "int") {
cout << DataParser::LEByte(CurrentColumnBytes); //custom function that tests length, converts to Little Endian int from the individual bytes.
}
if (DataType == "string") {
for (int i = 0; i < ColumnWidth; i++)
cout<<CurrentColumnBytes[i];
}
cout<<" | ";
}
推荐阅读
- angular - 将表格与顶部对齐 - 角度表格和垫卡
- intellij-idea - 无法在 build.gradle.kts 中准备好文件文本
- swift - Swift DateFormatter 不在项目上工作但在操场上工作?
- decision-tree - 基于方面的情感分析分类器 - 如何从分类器返回未知数的技术?
- javascript - AJAX 调用后 DataTables 不会运行渲染函数
- django - ImageFiled 无法在 django 中进行更新
- apache-zookeeper - 使用nmap检测zookeeper进程打开的端口
- batch-file - Windows批处理命令加密多个PGP文件
- java - 如何在不引用本地目录的情况下在 IntelliJ 中添加外部 jar 文件
- perl - Perl:如何在处理下一个之前等待 curl 步骤完成