问题
I'd like to read uint32_t integers from a file using the code below. ifstream only accepts a pointer to a char array. Is there another way to read uint32_t values using a code similar to the below?
int readCount;
uint32_t buffer[SIZE];
while ( fin.read( &buffer[0], SIZE)
|| (readCount = fin.gcount()) != 0 ) {
// some code
}
回答1:
Use a cast, e.g.:
if (fin.read(reinterpret_cast<char *>(buffer), sizeof buffer) &&
fin.gcount() == sizeof buffer)
{
// use buf
}
(Interpreting any object as an array of characters is expressly allowed, precisely for the purpose of I/O.)
来源:https://stackoverflow.com/questions/23418818/read-uint32-t-values-from-file