你的问题对我来说毫无意义,但这里有一堆关于如何读取二进制文件的随机信息:
struct myobject { //so you have your data
char weight;
double value;
};
//for primitives in a binary format you simply read it in
std::istream& operator>>(std::istream& in, myobject& data) {
return in >> data.weight >> data.value;
//we don't really care about failures here
}
//if you don't know the length, that's harder
std::istream& operator>>(std::istream& in, std::vector<myobject>& data) {
int size;
in >> size; //read the length
data.clear();
for(int i=0; i<size; ++i) { //then read that many myobject instances
myobject obj;
if (in >> obj)
data.push_back(obj);
else //if the stream fails, stop
break;
}
return in;
}
int main() {
std::ifstream myfile("input.txt", std::ios_base::binary); //open a file
std::vector<myobject> array;
if (myfile >> array) //read the data!
//well that was easy
else
std::cerr << "error reading from file";
return 0;
};
此外,如果您碰巧知道在哪里可以找到您要查找的数据,您可以使用 的.seek(position)
成员ifstream
直接跳到文件中的特定点。
哦,您只想将文件的前 12 个字节读取为 8 位整数,然后将接下来的 12 个字节读取为 int32_t?
int main() {
std::ifstream myfile("input.txt", std::ios_base::binary); //open a file
std::vector<int8_t> data1(12); //array of 12 int8_t
for(int i=0; i<12; ++i) //for each int
myfile >> data1[i]; //read it in
if (!myfile) return 1; //make sure the read succeeded
std::vector<int32_t> data2(3); //array of 3 int32_t
for(int i=0; i<3; ++i) //for each int
myfile >> data2[i]; //read it in
if (!myfile) return 1; //make sure the read succeeded
//processing
}