FreeRTOS+FAT ff_fread Read size

Hello
Please tell me about read size after ff_fread

I want read 100byte after write 10byte
But return read size = 10

Where should I fix it?
I attach the code screen of the write and read

@taki

ff_fread() returns the number of items read from the file and not really the size.

The return value is calculated as follows:

lResult = ( int32_t ) ( ulBytesRead / ulElementSize );

In your case, the number of items read (10) is less than the xItems value, which is 100. In that case, you can check the task’s errno to know the reason.

Thank you for reply

error = 0
Is the method correct?

Is there a way to read 100 bytes?

Seems like there is no error reported.

Did you verify if there was enough data to be read from the file?
Also, what’s the type of g_read_data?

A file that was 100 bytes before writing was 10 bytes when I checked the file size after writing.

If I write 10 bytes, does the file size become 10 bytes?

I thought I was rewriting the first 10 bytes of a 100-byte file.

What can I do to keep the remaining data?

When you open a file in write mode, only the data that you write remains there. This is standard file read/write semantics.

If you want to replace the first 10 bytes and keep the last 90 bytes as it is, you should do the following:

  1. Read all the 100 bytes in a buffer.
  2. Update the first 10 bytes in the buffer.
  3. Write the updated buffer to the file.

“Write” mode is defined to truncate the file, and start over.

To overwrite or append data, you need to open in APPEND mode, and then seek the location you want to write at.