0

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
}
Bob
  • 10,741
  • 27
  • 89
  • 143

1 Answers1

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.)

Kerrek SB
  • 464,522
  • 92
  • 875
  • 1,084
  • it is giving me error: invalid static cast from uint32_t – Bob May 02 '14 at 00:19
  • 1
    @Bob: Yes, sorry, it's supposed to be a `reinterpret_cast`. – Kerrek SB May 02 '14 at 00:20
  • @Bob: And if you end up with a partial read like you indicate in your code, you have to do the division yourself to figure out how many integers you can read.... – Kerrek SB May 02 '14 at 00:22
  • if gcount is less than buffer size, isn't it sufficient to read gcount() elements from buffer (last chunk)? – Bob May 02 '14 at 00:24