简体   繁体   English

Ascii .txt 文件到字节数组 - C++

[英]Ascii .txt file to array of bytes - C++

How can I convert an ascii .txt file into an array of bytes in c++?如何将 ascii .txt 文件转换为 C++ 中的字节数组? For example given this .txt file of XYZ Coordinates convert it to an array of bytes in floating point representation:例如,给定这个 XYZ 坐标的 .txt 文件,将其转换为浮点表示形式的字节数组:

253.9999929 58.0428367 -21.3930063253
.9999929 59.0435773 -21.2499391255
...

converts to转换为

bytes array[] = {
01000011,01111110,00000000,00000000,
01000010,01101000,00101011,11011101,
11000001,10101011,00100100,11100001,
00111111,01111111,11111111,10001001... etc

}

I thought of converting each number in the string to a floating point number and then extracting the binary representation;我想到将字符串中的每个数字转换为浮点数,然后提取二进制表示; but, im not sure if that's efficient.但是,我不确定这是否有效。 I need to do this on a large scale with a .txt file with 200000-1400000 lines of XYZ data.我需要使用包含 200000-1400000 行 XYZ 数据的 .txt 文件大规模执行此操作。

Thanks!谢谢!

Maybe this:也许这个:

struct xyz{
    float x;
    float y;
    float z;
};

istream& operator >>(istream & is, xyz & v) {
    return is >> v.x >> v.y >> v.z;
}

std::ifstream f{"floats.txt"};

vector<xyz> floats;
copy(istream_iterator<xyz>{f}, {}, inserter(floats, end(floats)));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM