Search code examples
visual-c++gccifstream

Use std::ifstream::read to count the number of character


Apparently this piece of code:

std::ifstream filev(path, std::ios::binary|std::ios::in);
unsigned long int nData = 0;
filev.read((char*)&nData, sizeof(nData) );

As @IgorTandetnik suggested is used to count the number of bytes present in a binary file thanks to the file header, in particular on Windows at the end of execution nData will contains the size of the file.

Now i'm wondering why this code on the same processor gives different result on different S.O.

Can someone give me an explanation why MSVC makes this code work?


Solution

  • This has nothing at all to do with file size (it's possible that your file happens to store its own size in its first 4 bytes, but it's not true of an arbitrary file, and in any case is beside the point).

    Your code exhibits implementation-defined behavior; in other words, it's non-portable. In particular, unsigned long int is 32 bit large on Windows and 64 bit on Linux, so you read first 4 bytes of a file sometimes, and first 8 bytes other times; naturally, this results in different values when those bytes are interpreted as a binary representation of an integer.

    Use unit32_t et al when you need integers of a particular bit size.

    This still leaves the fact that binary representation of an integer is itself implementation-defined, even if two implementations use integers of the same size. For example, you are in for a surprise if you ever run this code on a big-endian platform (luckily, those are somewhat rare these days), or one that doesn't use two's-complement representation (those are virtually non-existent).