Writing Text file on SD

this is my first post her and it is my first time to use SD module ,

what i am trying to do as follows : i want to make some calculation and write it in the text file till i reach specific number of calculation for example 10 times ,when i reach this number i will call file.close() to save the data and close the file . the problem is when i cut off the power before it reach 10 times all the data did not saved because the file.close() will not executed so how can i ensure that every time i write the data it will be saved even if i did not reach file.close() in case power cut-off before closing the file ??

i hope that i explained my problem clearly
thanks in advance

Call file.flush() maybe?

Not sure if that will work, but you could try it.

Not sure if that will work, but you could try it.

It will, but, if you are only writing a few records to the file, opening and closing the file each time is better. The time needed to open, write to a file , and close it increases as the file get larger. But a file with less than 1000 records, or 10000 characters, or so, will open. write, and close in next to no time.

when i try to open the file and write single data then close it and open it again to write the next data it override the last data so at the end of the loop the file will contain the last data only .. so how can i make it without loosing my data every time i open and close the file ??

No3man:
when i try to open the file and write single data then close it and open it again to write the next data it override the last data so at the end of the loop the file will contain the last data only .. so how can i make it without loosing my data every time i open and close the file ??

How are you opening the file? (In other words, condense you sketch down to an example that compiles and runs just enough to show your issue. Then post your code here inside code tags after using the IDE's auto-format menu tool.) It may be a simple syntax issue or forgotten argument to a function/method that can be solved either by yourself when you are condensing the code, or by one of us when we see your condensed code.

No3man:
when i try to open the file and write single data then close it and open it again to write the next data it override the last data so at the end of the loop the file will contain the last data only .. so how can i make it without loosing my data every time i open and close the file ??

Append as opposed to write?

EDIT:

On further inspection, I see that there is no file.append() but there is file.position(), file.seek() and file.size()
You may want to look at those.

UnoDueTre:

No3man:
when i try to open the file and write single data then close it and open it again to write the next data it override the last data so at the end of the loop the file will contain the last data only .. so how can i make it without loosing my data every time i open and close the file ??

Append as opposed to write?

EDIT:

On further inspection, I see that there is no file.append() but there is file.position(), file.seek() and file.size()
You may want to look at those.

Actually, I was hoping to see his code to confirm my suspicion that he is just using file.open(filename) structure instead of file.open(filename,mode) structure... (And verify that he is actually using the built-in SD library instead of some random vendor-supplied library.)

Also, I didn't feel like saying RTFM.

mode (optional): the mode in which to open the file, defaults to FILE_READ - byte. one of:

FILE_READ: open the file for reading, starting at the beginning of the file.
FILE_WRITE: open the file for reading and writing, starting at the end of the file.