简体   繁体   中英

sys/stat.h returning wrong size

I am using

  1. unistd.h
  2. sys/stat.h

and what is interesting is that st_size is bigger than the value returned after read() ? st.st_size returns 644 - read() returns 606 , Any thoughts?

The memory is allocated in multiples of blocks (st_blocks). The remaining bytes you noticed are due to block alignment.

If you check the value of st.st_blocks you will see that it is a good divisor for 644 , while not to 606.

So, if you want to know the real size occupied with data, do not use st_size .

Size of file and size of stream may not match. For example, when you open file in text mode, OS can translate multi-byte EOL sequences into single char. This is usually visible on Windows, but if you have a Windows file on Linux, the effect can be the same.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM