2

I've got an application that requires moving to specific points on a file on an SD card, reading, modifying and then writing an update. The file is fixed size. The code has no problems moving to the specific offsets required and then reading in blocks of data.

However, when the code does a seek() to the next desired location, then a write() and a flush(), the data is not written. If my code seeks back to the start of the block just written and reads it back, it does not contain the new data.

The file is definitely open for writing, and the write operation indicates it wrote all the bytes. No calls are indicating any error.

Since reads and seeks are working I'm assuming this is not a problem with wiring of the device to the Arduino Mega. The files range from around 100K to almost 2M each, and the code needs to access the entire content of the file, but almost never sequentially. Writes are almost always non-sequential as well.

Is it necessary to do a close after each write instead of the flush?

Does anyone have any suggestions of things I should be investigating?

Bob

Bob
  • 21
  • 2
  • What filesystem layer are you using? – Ignacio Vazquez-Abrams Jun 20 '15 at 04:20
  • I'm using the File layer. Ie, File myfile; myfile.open(), myfile.seek(), myfile.write(), etc. – Bob Jun 20 '15 at 11:39
  • I just ran an experiment where the code seeks to a record in the middle of the file, writes a new record and then closes the file. Next, it re-opens the file, seeks to the record and reads in back... the changes are not there. – Bob Jun 20 '15 at 11:58
  • 1
    Can u post example of your code? – Iron Fist Jun 20 '15 at 12:39
  • This may sound trivial, but did you check if write protect is enabled? I'm not familiar with the library your using, but from the information I gleaned from a quick google search, it seems like it simply returns the number of bytes written. In some file systems, the number of bytes written doesn't always mean that it was a successful write. It could just mean that x number of bytes were written to a buffer, pipe, or whatever. – Mlagma Jul 01 '15 at 23:56

0 Answers0