Skip to main content
Luca G.
Associate III
June 5, 2020
Solved

FatFS file size: what value to use?

  • June 5, 2020
  • 3 replies
  • 3644 views

Dear all,

I'm using the FatFS generated by STM32CubeMX and I have one question about file size:

I'm using f_stat functionand read the file size from fsize field of FILINFO structure; when I open a file I can read the file size from obj.objsize field of FIL structure.

I have seen that  the value is calculate differently but the value it's the same.

Is it possible to use the FIL structure field or is it recommended to use the FILEINFO field because in some cases the value may not be correct?

Thanks in advance.

Luca G.

This topic has been closed for replies.
Best answer by berendi

> the value is calculate differently

Both of them contains the value of the file size field in the directory entry. There is no ther way to calculate it (I'm not sure about exFat).

> Is it possible to use the FIL structure field or is it recommended to use the FILEINFO field

fp->obj is an internal structure which might change in a future version.

Use f_size() if you open the file anyway, it is the most efficient and documented way as long as the file is open. It is the same as fp->obj.objsize, but it is documented, so you can expect that this interface remains stable in the future.

Use f_stat() if you need the information before opening the file.

3 replies

berendi
berendiBest answer
Principal
June 5, 2020

> the value is calculate differently

Both of them contains the value of the file size field in the directory entry. There is no ther way to calculate it (I'm not sure about exFat).

> Is it possible to use the FIL structure field or is it recommended to use the FILEINFO field

fp->obj is an internal structure which might change in a future version.

Use f_size() if you open the file anyway, it is the most efficient and documented way as long as the file is open. It is the same as fp->obj.objsize, but it is documented, so you can expect that this interface remains stable in the future.

Use f_stat() if you need the information before opening the file.

Tesla DeLorean
Guru
June 5, 2020

Always used f_size() on an open file since the function/macro was available.

Secondarily used bytes read from f_read() to determine if end of file occurred prematurely.

Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..
Luca G.
Luca G.Author
Associate III
June 5, 2020

Many thanks for the clarifications.

Regards.