logFile.close

Hi

i have a question about using a file on you SD cart

normally you open a file SD.open..... than write some data in it , and when you done close the file logFile.close()
But in my program it is opened at 00:00h and closed at 23:55h,

If i reset or powerdown between that time, the arduino will NOT give a logFile.close() .
Could the file then be corrupted or get corrupted, or ??? of will the pointer be lost , where file is ended (last written character)

i have done this a couple of times , not closing file and after start up the arduino opened it same file again for writing data , no problem, no currupt file.. is there some other program closing the file , or is thr SD cart protected against this

if a file could get corrupted , if there a check for this

geetings

But in my program it is opened at 00:00h and closed at 23:55h,

Why? It should be opened when you need to write to it, and closed when you are done writing.

If i reset or powerdown between that time, the arduino will NOT give a logFile.close() .

That's not good.

Could the file then be corrupted or get corrupted

Yes.

i have done this a couple of times , not closing file and after start up the arduino opened it same file again for writing data , no problem, no currupt file.. is there some other program closing the file , or is thr SD cart protected against this

You've been lucky. I don't advise continuing a bad practice, though.

if a file could get corrupted , if there a check for this

Sure. Define corrupted. Detect where the files is corrupted, and write an EOF in that location. The file will no longer be corrupted.

PaulS:

But in my program it is opened at 00:00h and closed at 23:55h,

Why? It should be opened when you need to write to it, and closed when you are done writing.

If i reset or powerdown between that time, the arduino will NOT give a logFile.close() .

That's not good.

Could the file then be corrupted or get corrupted

Yes.

i have done this a couple of times , not closing file and after start up the arduino opened it same file again for writing data , no problem, no currupt file.. is there some other program closing the file , or is thr SD cart protected against this

You've been lucky. I don't advise continuing a bad practice, though.

if a file could get corrupted , if there a check for this

Sure. Define corrupted. Detect where the files is corrupted, and write an EOF in that location. The file will no longer be corrupted.

i write every 5 minutes data to it, so thats the reason i do not close it every time

i write every 5 minutes data to it, so thats the reason i do not close it every time

That's not a reason. That's an excuse.

PaulS:

i write every 5 minutes data to it, so thats the reason i do not close it every time

That's not a reason. That's an excuse.

its the easiers way.. to leave it open

So your advice open file write data and close it .. next 5 min open it again etc.. OK ., so window for crashing file is very small(only during writing)
But will the program or file know where the put the next line, is the pointer where the last character is written alway known, when i open the file again?

it see on the arduino.cc page

Opening/Closing files
When you use file.write(), it doesn't write to the card until you flush() or close(). Whenever you open a file, be sure to close it to save your data.

So a flush will save it directly , what will do en close instruction , save an EOF???

its the easiers way.. to leave it open

Sure, but it's not the right way.

So your advice open file write data and close it .. next 5 min open it again etc.. OK ., so window for crashing file is very small(only during writing)

Yes, it is.

But will the program or file know where the put the next line, is the pointer where the last character is written alway known, when i open the file again?

Depends on how you open the file. If you open in append mode, then, yes, the write pointer is positioned at the end of the file.

So a flush will save it directly , what will do en close instruction , save an EOF???

Each time you call the write() (or print() or println()) method, data is written to a buffer. The flush() method actually causes a write to the file, copying the buffer contents into the file. The close() method calls flush(). Closing the file does write an EOF into the file, while flush() does not.

PaulS:

its the easiers way.. to leave it open

Sure, but it's not the right way.

So your advice open file write data and close it .. next 5 min open it again etc.. OK ., so window for crashing file is very small(only during writing)

Yes, it is.

But will the program or file know where the put the next line, is the pointer where the last character is written alway known, when i open the file again?

Depends on how you open the file. If you open in append mode, then, yes, the write pointer is positioned at the end of the file.

So a flush will save it directly , what will do en close instruction , save an EOF???

Each time you call the write() (or print() or println()) method, data is written to a buffer. The flush() method actually causes a write to the file, copying the buffer contents into the file. The close() method calls flush(). Closing the file does write an EOF into the file, while flush() does not.

OK thx for information

so only difference is the EOF , in my case . This could course a corrupt file , because it is not properly closed

file corrupt dectetion
can i detect it by . if file SD.exist then , try SD.open

a File object referring to the opened file; if the file couldn't be opened, this object will evaluate to false in a boolean context, i.e. you can test the return value with "if (f)".