visual c++ - Use std::ifstream::read to count the number of character -


apparently 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 used count number of bytes present in binary file file header, in particular on windows @ end of execution ndata contains size of file.

now i'm wondering why code on same processor gives different result on different s.o.

can give me explanation why msvc makes code work?

this has nothing @ file size (it's possible file happens store own size in first 4 bytes, it's not true of arbitrary file, , in case beside point).

your code exhibits implementation-defined behavior; in other words, it's non-portable. in particular, unsigned long int 32 bit large on windows , 64 bit on linux, read first 4 bytes of file sometimes, , first 8 bytes other times; naturally, results in different values when bytes interpreted binary representation of integer.

use unit32_t et al when need integers of particular bit size.

this still leaves fact binary representation of integer implementation-defined, if 2 implementations use integers of same size. example, in surprise if ever run code on big-endian platform (luckily, rare these days), or 1 doesn't use two's-complement representation (those virtually non-existent).


Comments

Popular posts from this blog

routing - AngularJS State management ->load multiple states in one page -

python - GRASS parser() error -

Swift game error message -