首页 > 解决方案 > 将 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_tandwint_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;
    }
}

标签: c++parsingbinaryfiles

解决方案


好的,所以问题是您需要将文件读入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<<" | ";    
}

推荐阅读