File read after calling write_all() returns empty data in tokio

There has been a bug in my program that has caused lots of issues with package managers. When my program starts up, it reads a config file. If the file does not exist, it will create and write the file with some default data, then read it again. Unfortunately when reading the file immediately after writing it, it always returns an empty string (and causes serde to fail).

I was able to replicate the bug in this playground:

How can I fix this?

Missing a seek. Line #14.

You may be better served closing the file after writing then opening just for reading.

If you want to read what you just wrote, you need to rewind the file. Otherwise, you are reading the zero bytes located after the bytes you just wrote.


Interesting, I didn't realise that! Thanks for the help!

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.