# FreeRTOS＋FAT ff\_fread Read size

**URL:** <https://forums.freertos.org/t/freertos-fat-ff-fread-read-size/21836>\
**Category:** Renesas\
**Created:** [November 13, 2024, 7:57am UTC](https://forums.freertos.org/t/freertos-fat-ff-fread-read-size/21836 "2024-11-13T07:57:43Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![taki](https://avatars.discourse-cdn.com/v4/letter/t/ccd318/32.png) [@taki](https://forums.freertos.org/u/taki)\
**Post date:** [November 13, 2024, 7:57am UTC](https://forums.freertos.org/t/freertos-fat-ff-fread-read-size/21836/1 "2024-11-13T07:57:43Z")

</div>

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

 ![DataSize](https://d2v6vdsk2p900z.cloudfront.net/original/2X/f/f1220d77eda5859455fbd0dc969cefdc218d47f6.jpeg)

---

<div class="post-metadata">

**Author:** ![tony-josi-aws](https://avatars.discourse-cdn.com/v4/letter/t/a6a055/32.png) [@tony-josi-aws](https://forums.freertos.org/u/tony-josi-aws)\
**Post date:** [November 13, 2024, 8:37am UTC](https://forums.freertos.org/t/freertos-fat-ff-fread-read-size/21836/2 "2024-11-13T08:37:56Z")

</div>

@taki

[`ff_fread()`](https://www.freertos.org/Documentation/03-Libraries/05-FreeRTOS-labs/04-FreeRTOS-plus-FAT/Standard-API/ff_fread) returns the number of items read from the file and not really the size.

The return value is calculated as follows:

```c
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](https://www.freertos.org/Documentation/03-Libraries/05-FreeRTOS-labs/04-FreeRTOS-plus-FAT/Standard-API/errno) to know the reason.

---

<div class="post-metadata">

**Author:** ![taki](https://avatars.discourse-cdn.com/v4/letter/t/ccd318/32.png) [@taki](https://forums.freertos.org/u/taki)\
**Post date:** [November 13, 2024, 9:15am UTC](https://forums.freertos.org/t/freertos-fat-ff-fread-read-size/21836/3 "2024-11-13T09:15:49Z")

</div>

Thank you for reply

error = 0  
Is the method correct?

 ![error](https://d2v6vdsk2p900z.cloudfront.net/original/2X/2/26eb110e068a06b27a730e3c75c3207c453de438.jpeg)

Is there a way to read 100 bytes?

---

<div class="post-metadata">

**Author:** ![tony-josi-aws](https://avatars.discourse-cdn.com/v4/letter/t/a6a055/32.png) [@tony-josi-aws](https://forums.freertos.org/u/tony-josi-aws)\
**Post date:** [November 13, 2024, 9:49am UTC](https://forums.freertos.org/t/freertos-fat-ff-fread-read-size/21836/4 "2024-11-13T09:49:02Z")

</div>

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`?

---

<div class="post-metadata">

**Author:** ![taki](https://avatars.discourse-cdn.com/v4/letter/t/ccd318/32.png) [@taki](https://forums.freertos.org/u/taki)\
**Post date:** [November 14, 2024, 12:21am UTC](https://forums.freertos.org/t/freertos-fat-ff-fread-read-size/21836/5 "2024-11-14T00:21:08Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![aggarg](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/aggarg/32/4245_2.png) [@aggarg](https://forums.freertos.org/u/aggarg)\
**Post date:** [November 14, 2024, 3:52am UTC](https://forums.freertos.org/t/freertos-fat-ff-fread-read-size/21836/6 "2024-11-14T03:52:36Z")

</div>

> [@taki](#):
>
> If I write 10 bytes, does the file size become 10 bytes?

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

> [@taki](#):
>
> What can I do to keep the remaining data?

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.

---

<div class="post-metadata">

**Author:** ![richard-damon](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/richard-damon/32/38_2.png) [@richard-damon](https://forums.freertos.org/u/richard-damon)\
**Post date:** [November 14, 2024, 10:44pm UTC](https://forums.freertos.org/t/freertos-fat-ff-fread-read-size/21836/7 "2024-11-14T22:44:05Z")

</div>

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