Hi, i'm after reading a block of data from a file which is in binary, and copying the data to a structure. The struture is exactly alligned to match the data within the file. Could anyone advise us on the type of function to use.... Thanks.
Printable View
Hi, i'm after reading a block of data from a file which is in binary, and copying the data to a structure. The struture is exactly alligned to match the data within the file. Could anyone advise us on the type of function to use.... Thanks.
Since it's C++ and it's a binary file: ifstream::read()
Code:STRUCTURE MyStructure;
ifstream ReadFile;
ReadFile.open("MyFile.dat", ios::in | ios::binary);
if(!ReadFile.fail())
{
ReadFile.read((char*)&MyStructure, sizeof(STRUCTURE));
ReadFile.close();
}
right that looks great thanks, why the typecasting to a char pointer though?
Since the ifstream class have no idea what datatypes you may have created, the only type it supports to read/write are char. Probably beacuse one char is 1 byte (most of the time at least).Quote:
Originally posted by subdene
right that looks great thanks, why the typecasting to a char pointer though?
Otherwise you had to overload the read function yourself for every datatype you have made.
right ok thanks.
Magos, i've implemented the code, but all i get in the structure is garbage.... any ideas? Binary file is ok.... can't see what it could be.
Post some code.
all right got it sorted now thanks, i was reading too far into the binary file.