Skip to main content
Visitor II
June 5, 2020
Solved

FatFS file size: what value to use?

  • June 5, 2020
  • 2 replies
  • 3587 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.

    2 replies

    berendiAnswer
    Visitor II
    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.

    Graduate II
    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.

    Luca G.Author
    Visitor II
    June 5, 2020

    Many thanks for the clarifications.

    Regards.