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,254
  • 10
  • 29
  • 41
3ORZ
  • 17
  • 1
  • 4
  • Your question is not containing any info, data or facts that make it comprehensible. So add more data, facts and info. – MatsK Sep 27 '17 at 03:32
  • I have file Config.txt that contain this DisplayBrightness=100 and them I change 100 to 60 via c++ program and remove plug off. When I start my Rpi this content is disappeared and size of my Config.txt is zero. However if type sudo reboot or sudo shutdown this line of content still there !! – 3ORZ Sep 27 '17 at 03:43
  • Similar: https://raspberrypi.stackexchange.com/questions/32146/read-write-filesystem-no-problem-when-halting-the-pi-by-removing-the-power-cor – BowlOfRed Sep 27 '17 at 04:14
  • Yes similar. Is there anyway to handle this without using RC circuit or install NARD ? – 3ORZ Sep 27 '17 at 04:55
  • You should probably update Raspbian, and see if that fixes your issue. – NerdOfLinux Sep 27 '17 at 16:51

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
  • 2,915
  • 1
  • 8
  • 20
  • I use fstream for file writing. Can I use flush instead of fflush ? – 3ORZ Sep 27 '17 at 07:54
  • @3ORZ Yes, its the same thing (`fflush(file) == fstream.flush()`) fflush is the actual api call. Sync is more critical. Roughly speaking `flush` = write out to kernel buffer, `sync` = write out to disk. – crasic Sep 27 '17 at 13:57
  • I tried both flush and sync before fstream.close() and it doesn't work :( – 3ORZ Sep 28 '17 at 11:02
  • close then sync. – crasic Sep 28 '17 at 17:16