cancel
Showing results for 
Search instead for 
Did you mean: 

FatFS file size: what value to use?

Luca G.
Associate III

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.

1 ACCEPTED SOLUTION

Accepted Solutions
berendi
Principal

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

View solution in original post

3 REPLIES 3
berendi
Principal

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

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
Up vote any posts that you find helpful, it shows what's working..
Luca G.
Associate III

Many thanks for the clarifications.

Regards.