Pregunta

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?

¿Fue útil?

Solución

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.

Otros consejos

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.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top