1

I tried to create a config file on my IoT device that is used for storing content when a user changes a setting via a menu. I found that every time the content of file is changed and then the system is unplugged, all of content in my config file disappears. This doesn't happen when I reboot or shutdown correctly but my device cannot avoid the user unplugging it without shutdown or reboot.

My Raspbian version is November 2016 Anyone know what I should do?

techraf
  • 4,353
  • 10
  • 32
  • 43
3ORZ
  • 17
  • 1
  • 4

2 Answers2

1

As I understand. You write a file to disk. When the device is shutdown. File is there. When device loose power ( ripp out the powerchorde) the file is gone. I'm not a cc+ programmer. But I believe this is due to not flushing file content to disk and closing the file pointer. (In your application) If you run shutdown/reboot. The OS will try to do the magic for you.

jensse
  • 11
  • 1
0

Use Some Combination of sync, fsync, flush


Flash is slow, files are not immediately written to disk, unless you force the operation it may be longer than you expect.

In shell, bash use sync, a system call

sync - synchronize data on disk with memory

https://linux.die.net/man/2/sync

https://linux.die.net/man/3/sync

c, c++, python, etc. you need to first (f)flush (after write). Some language structures have convenience methods for doing this (e.g. in c++ ostream::flush )

fflush - flush a stream

https://linux.die.net/man/3/fflush

and then fsync (or can also sync ) to write to disk

fsync, fdatasync - synchronize a file's in-core state with storage device

https://linux.die.net/man/3/fsync

NOTE Note that flush is called automatically on close file or if process quits, sync is not

crasic
  • 3,033
  • 1
  • 11
  • 20